0

我正在使用带有示例代码的 Sherlock 库在应用程序中工作。

我想在 ActionBar 的右上角添加菜单项,但在此过程中出现异常。我使用下面的代码在SherlockActionBar中添加Menu

public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
        com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.layout.activity_main, menu);
        return super.onCreateOptionsMenu(menu);

    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);

        switch (item.getItemId()) {
        case R.id.phone:
            Toast.makeText(getBaseContext(), "You selected Phone",
                    Toast.LENGTH_SHORT).show();
            break;

        case R.id.computer:
            Toast.makeText(getBaseContext(), "You selected Computer",
                    Toast.LENGTH_SHORT).show();
            break;

        case R.id.gamepad:
            Toast.makeText(getBaseContext(), "You selected Gamepad",
                    Toast.LENGTH_SHORT).show();
            break;

        case R.id.camera:
            Toast.makeText(getBaseContext(), "You selected Camera",
                    Toast.LENGTH_SHORT).show();
            break;

        case R.id.video:
            Toast.makeText(getBaseContext(), "You selected Video",
                    Toast.LENGTH_SHORT).show();
            break;

        case R.id.email:
            Toast.makeText(getBaseContext(), "You selected EMail",
                    Toast.LENGTH_SHORT).show();
            break;

        }
        return true;
    }

编译代码时出现以下异常:

Uncaught handler: thread main exiting due to uncaught exception
 java.lang.RuntimeException: Expecting menu, got RelativeLayout
    at com.actionbarsherlock.view.MenuInflater.parseMenu(MenuInflater.java:145)
    at com.actionbarsherlock.view.MenuInflater.inflate(MenuInflater.java:112)
    at in.wptrafficanalyzer.actionbarsherlocknavtabswipe.MainActivity.onCreateOptionsMenu(MainActivity.java:110)
    at android.support.v4.app.Watson.onCreatePanelMenu(Watson.java:45)
    at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:559)
    at com.actionbarsherlock.internal.ActionBarSherlockCompat.preparePanel(ActionBarSherlockCompat.java:479)
    at com.actionbarsherlock.internal.ActionBarSherlockCompat.dispatchInvalidateOptionsMenu(ActionBarSherlockCompat.java:272)
    at com.actionbarsherlock.internal.ActionBarSherlockCompat$1.run(ActionBarSherlockCompat.java:984)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4363)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
4

1 回答 1

2

您正在膨胀布局 ( R.layout.activity_main)onCreateOptionsMenu()

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.layout.activity_main, menu);
    return super.onCreateOptionsMenu(menu);
}

把它改成这样:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.some_menu_in_your_menu_folder, menu);
    return super.onCreateOptionsMenu(menu);
}

这是您将在res->menu文件夹中创建的菜单

于 2013-03-29T05:41:41.687 回答