简体   繁体   English

如何从另一个类调用菜单对象?

[英]How to call the menu objects from another class?

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. 这是我的MainActivity类。

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 这是我的SecondActivity课程

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 . 不是您在onCreate尝试的代码。 If you want the menu item responses to be the same, copy the onOptionsItemSelected method too. 如果希望菜单项的响应相同,则也复制onOptionsItemSelected方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM