活动上的相同选项菜单,无效的方法声明;返回类型必填

问题描述 投票:1回答:1

我的项目中有多个活动,它们使用相同的选项菜单。因此,我试图为选项菜单功能使用单个类,但是当我尝试调用函数时,出现此错误:

 Invalid method declaration; return type required

当我将函数分配给变量时,出现以下错误:

Error -- Expression expected

boolean optionCreate = super.onCreateOptionsMenu(menu);

和:

Error -- Cannot resolve symbol 'item'

boolean optionSelect = super.onOptionsItemSelected(item);

创建这些字段会使使用选项菜单的活动无法加载。

我正在使用的代码是:

MainActivity.java


public class mainActivity extends OptionsMenu {

    ListView listView;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
        setSupportActionBar(myToolbar);
        getSupportActionBar().setTitle("Pick A Room");

        listView = (ListView) findViewById(R.id.listview);

        Intent intent = getIntent();
        String name = intent.getStringExtra("NAME");


        final ArrayList<String> arrayList = new ArrayList<>();

        arrayList.add("Big Fish");
        arrayList.add("Little Fish");
        arrayList.add("Cardboard Box");


        ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);

        listView.setAdapter(arrayAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int index, long id) {
                openDialog();
            }
        });
    }

    public void openDialog(){
        TryAgainErrDialog errDialog = new TryAgainErrDialog();
        errDialog.show(getSupportFragmentManager(), "error dialog");
    }


    boolean optionCreate = super.onCreateOptionsMenu(menu);
    boolean optionSelect = super.onOptionsItemSelected(item);
}

OptionsMenu.java

public class OptionsMenu extends AppCompatActivity {

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.appbar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.action_back:

                return true;
            case R.id.action_songQueue:

                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

}

android android-activity android-optionsmenu
1个回答
0
投票

无需调用那些方法。它将由系统自动调用。从您的代码中删除这些代码。

boolean optionCreate = super.onCreateOptionsMenu(menu);
boolean optionSelect = super.onOptionsItemSelected(item);
© www.soinside.com 2019 - 2024. All rights reserved.