i’am beginner and i’am trying to make a simple calculator for android but he give me a syntax error
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
final EditText e = (EditText)findViewById(R.id.value);
final EditText e2 = (EditText)findViewById(R.id.value2);
getMenuInflater().inflate(R.menu.activity_main, menu);
Toast.makeText(this,"Welcome",Toast.LENGTH_LONG).show();
Button welcome = (Button)findViewById(R.id.x);
welcome.setText("push ");
welcome.setonclickListener(new View.onclickListener() {
@Override
public void onclick(View v) {
int v1 = Integer.parseInt(e.getText().toString());
int v2 = Integer.parseInt(e2.getText().toString());
int v3 = v1 + v2 ;
Toast.makeText(MainActivity.this,"= " + v3, Toast.LENGTH_LONG).show();
return true;
}
});
}
}
Use
OnClickListener(). You don’t have to specifyView. Also, make sure you use a capital ‘C’ in the overriddenonClick()method. Make sure you includeimport android.view.View.OnClickListener;in your imports section. Eclipse should this for you, but if it’s not or you are using Eclipse, add it manually to the top of the class. If you simply do the following, that should work for you:——–EDITED FOR FULL EXAMPLE——–
In your project, you have your main activity. That activity should use a layout from an XML resource. That XML resource likely looks something like this:
In your activity class, you have to set that resource as the content view for the activity, using
setContentView(resID). Say your XML file is called helloworld.xml, you would executesetContentView(R.layout.helloworld)in the activity’sonCreate(Bundle s)method right after callingsuper.onCreate(s).Once you have set the activity’s view, you can access that layout’s elements (EditTexts, Buttons, etc). To do this, you’ll have to create EditText and Button objects (which you were already doing in your posted code, we just have to do them elsewhere instead). Continuing with my example, you could do the following in your
onCreate(Bundle s)function:If you want to change what the button does based on a menu item selection, you have to override
onOptionsItemSelected(MenuItem item)in addition toonCreateOptionsMenu(Menu menu).onCreateOptionsMenu(Menu menu)simply creates the menu for the menu button to open.onOptionsItemSelected(MenuItem item)actually decides what to do when you select a menu item.See the tutorial at this page for full rundown http://developer.android.com/guide/topics/ui/menus.html, but here are their examples with some explanation. These examples are not from my example app above but from the Android Developer API pages. I strongly recommend you go over their tutorial.
All you have to do in
onCreateOptionsMenu(Menu menu)is tell Android where to get the menu and to inflate that resource. This means there is an XML file calledgame_menuin themenufolder of theresfolder in your project’s directory.onOptionsItemSelected(MenuItem item)is where the meat of the menu logic goes, letting you perform different tasks based on which menu item get’s selected. In this example, thegame_menuXML file mentioned in the above function has menu items callednew_gameandhelp.