These are codes, how to pass the menu object from one class to another? What's wrong with my code?
This is my MainActivity class.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
String msg = " ";
switch (item.getItemId()){
case R.id.action_settings:
msg = "Settings";
break;
case R.id.action_report:
msg= "Report";
break;
}
Toast.makeText(this, msg + "Checked", Toast.LENGTH_LONG).show();
return super.onOptionsItemSelected(item);
}
This is my SecondActivity class
public class Income extends AppCompatActivity{
View_Expenses v = new View_Expenses();
@Override
protected void onCreate(Bundle savedInstanceState) {
v.onCreateOptionsMenu(R.menu.main_menu); //Here have problem
}
Copy the same code you used in the first activity in the second one,
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
not the code you tried in onCreate
. If you want the menu item responses to be the same, copy the onOptionsItemSelected
method too.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.