eclipse android application syntax error - java

i just started to use eclipse, and im trying to do my first android application. But apparently something is wrong with my syntax error , and i dont know how can i fix it or where did i do wrongly. so i hope someone here can help me. Thanks :) pardon me for my bad english.
Those underlined in red are errors but i dont know how can i fix them.
starts below here
Here is my .java
public class Category extends Activity { <---(**this sign is underlined in red**)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
Button switchButton = (Button) findViewById(R.id.button1);
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cake.class);
startActivity(intent);
Button switchButton = (Button) findViewById(R.id.button2);
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cookie.class);
startActivity(intent);
};
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.category, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
} ^
(**this sign is underlined in red**)
};

Unfortunately you're trying to initialize your button listener in another button listener. That's wrong.
Corrected:
Button switchButton = (Button) findViewById(R.id.button1);
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cake.class);
startActivity(intent);
};
});
Button switchButton2 = (Button) findViewById(R.id.button2);
switchButton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cookie.class);
startActivity(intent);
};
});

Related

Android Studio keep getting "Error:(60, 12) error: method does not override or implement a method from a supertype"

I keep getting this error "Error:(60, 12) error: method does not override or implement a method from a supertype" on my last 2 #Overrides im not sure where i went wrong, any help would be helpful. I added the Button billspliting and everything then when to poop. I try to build the APK but I keep getting the two over ride errors.
private TextView totaltextview;
private EditText PercentageTxt;
private EditText Numbertxt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
totaltextview = (TextView) findViewById(R.id.Totaltextview);
PercentageTxt = (EditText) findViewById(R.id.PercentageTxt);
Numbertxt = (EditText) findViewById(R.id.Numbertxt);
Button CalcBtn = (Button) findViewById(R.id.CalcBtn);
CalcBtn.setOnClickListener(new View.OnClickListener() {
#SuppressLint("SetTextI18n")
#Override
public void onClick(View v) {
float percentage = Float.parseFloat(PercentageTxt.getText().toString());
float dec = percentage / 100;
float total = dec * Float.parseFloat(Numbertxt.getText().toString()) + Float.parseFloat(Numbertxt.getText().toString());
totaltextview.setText(Float.toString(total));
Button billspliting = (Button) findViewById(R.id.Button123);
billspliting.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(First.this, Myotheractivity.class);
startActivity(intent);
}
});
}
#Override
public boolean OnCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean OnOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return onOptionsItemSelected(item);
}
;
});
}}
Take OnCreateOptionsMenu and OnOptionsItemSelected outside of CalcBtn.setOnClickListener(new View.OnClickListener() as these method are present in Activity not in OnClickListener.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
totaltextview = (TextView) findViewById(R.id.Totaltextview);
PercentageTxt = (EditText) findViewById(R.id.PercentageTxt);
Numbertxt = (EditText) findViewById(R.id.Numbertxt);
Button CalcBtn = (Button) findViewById(R.id.CalcBtn);
CalcBtn.setOnClickListener(new View.OnClickListener() {
#SuppressLint("SetTextI18n")
#Override
public void onClick(View v) {
float percentage = Float.parseFloat(PercentageTxt.getText().toString());
float dec = percentage / 100;
float total = dec * Float.parseFloat(Numbertxt.getText().toString()) + Float.parseFloat(Numbertxt.getText().toString());
totaltextview.setText(Float.toString(total));
Button billspliting = (Button) findViewById(R.id.Button123);
billspliting.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(First.this, Myotheractivity.class);
startActivity(intent);
}
});
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return onOptionsItemSelected(item);
}

unfortunately GeoQuiz has stopped working in bignerdranch

When I try to run my app on the emulator I have a tab message saying
"unfortunately,GeoQuiz has stopped working" .......
GeoQuiz is my app's name .......
here is my activity class in the QuizActivity.java file
public class QuizActivity extends ActionBarActivity {
private Button mTrueButton;
private Button mFalseButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
mTrueButton=(Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this,R.string.correct_toast,Toast.LENGTH_SHORT).show();
}
});
mFalseButton=(Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT).show();
}
});
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_quiz, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I have no images at all in my app
and the min sdk version for this app is API 16:android 4.1 (jelly bean)
and the targeted version is API 21:android 5.0 (Lollipop)
you dont call setContentView at the right place:
you call it at the end of the onCreate method! You have to move these two lines to the beginning of the onCreate Method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
because of that you get a NullPointerException here:
mTrueButton=(Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() { // NPE

java.lang.NullPointerException and Unknown Source

I'm having a hard time fixing some issues with my code. I tested it on my phone and it works good. But if I test it on a Galaxy Tab 4, it gives me java.lang.NullPointerException and unknown source in the onCreate() method.
public class MainActivity extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainxml);
Button butns = (Button) findViewById(R.id.sb1);
butns.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), Sub1.class);
startActivity(intent);
}
});
Button butnss = (Button) findViewById(R.id.sb2);
butnss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), Sub2.class);
startActivity(intent);
}
});
Button butsns = (Button) findViewById(R.id.sb3);
butsns.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), Sub3.class);
startActivity(intent);
}
});
Button quiz = (Button) findViewById(R.id.quiz);
quiz.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), QuizActivity.class);
startActivity(intent);
}
});
ImageButton butsn1s = (ImageButton) findViewById(R.id.info);
butsn1s.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), Info.class);
startActivity(intent);
}
});
ImageButton btn = (ImageButton) findViewById(R.id.fb);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/pages/SharpMind-software/540118499454551?ref=hl"));
startActivity(myWebLink);
}
});
ImageButton cnt = (ImageButton) findViewById(R.id.count);
cnt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent count = new Intent(getApplicationContext(),Count.class);
startActivity(count);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.settings:
Intent settings = new Intent(getApplicationContext(), Settings.class);
startActivity(settings);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
And below is the error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sharpmind.Romana_pentru_BAC_PRO/com.sharpmind.Romana_pentru_BAC_PRO.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.access$900(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5479)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.sharpmind.Romana_pentru_BAC_PRO.MainActivity.onCreate(Unknown Source)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
... 11 more
Thanks.
Have a nice day!
Try to change the package name, it happens that some package names are not accepted by android. I don't know why but it even doesn't accept com.android.wifi
Faced same problem few days back and successfully solved by using a different package name
and make sure you use 'lower cases' and add com in the starting.
Thanks for help, now if the problem appears again I will know what to try.
I've found it. My main.xml layout large resource was not containing one of the buttons declared above.
I might look like a fool now, but it happens.
Thanks again for answers and comments.

2nd layout Button is not working

This is my First Question & I'm a new android learner,
Suddenly in my ellipse 2nd layout button is not working;
after pressing button of my first layout it working & going to second layout.
But in 2nd layout my Button is not working, I tested it very simple code. Please check
public class MainActivity extends Activity {
Button b1,b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
//setContentView(R.layout.activity_main_activity2); Need to remove
startActivity(new Intent(MainActivity.this,MainActivity2.class));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
This is my 2nd activity code
public class MainActivity2 extends Activity {
EditText ed;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
ed = (EditText) findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button21);
button.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "Working", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity2, menu);
return true;
}
}
*You are simply changing the layout on first activity using setContentView(). You are not navigating from one activity to another.
Either you can register the button of layout 2 by onClicklistener after setting it on activity.
b1.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
setContentView(R.layout.activity_main_activity2);
button = (Button) findViewById(R.id.button21);
button.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),"Working",Toast.LENGTH_SHORT).show();
}
});
}
});
or you can goto your other activity
b1.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// setContentView(R.layout.activity_main_activity2); remove
startActivity(new Intent(MainActivity.this,MainActivity2.class));
}
});

Button takes to wrong location

So i am having some trouble with my application, bt3 takes me to bt2 location when i dont want it to, Can someone please explain why?
public class MainActivity extends Activity {
Button bt, bt2, bt3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button) findViewById(R.id.button1);
bt2 = (Button) findViewById(R.id.button2);
bt3 = (Button) findViewById(R.id.button3);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
integrator.initiateScan();
}
});
bt2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this, location1.class);
startActivity(intent);
}
});
bt3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this, location2.class);
startActivity(intent);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
//super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
} else if (resultCode == RESULT_CANCELED) {
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
To clarify what each button is suppose to do:
button1 is supose to start a scan,
button2 is supose to take me to location1,
button3 is supose to take me to location2.
You could try implementing the onClick methods for each button in the XML instead, see if that changes anything.

Categories

Resources