Android,OptionMenu ResourceNotFoundException;什么时候切换到风景

Android,OptionMenu ResourceNotFoundException; when switch to landspace

当我的 android 应用以纵向屏幕启动时,操作栏中有选项菜单;但是当切换到 landspace 时,我发现资源未找到异常。 inflater.inflate(R.menu.main, 菜单) 异常;

为什么我会收到此错误,谢谢

这是我的代码

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    try {
        inflater.inflate(R.menu.main, menu); //Exception is here
    }
    catch(Exception ex){

        return super.onCreateOptionsMenu(menu);
    }

    // Here we get the action view we defined
    menuItem = menu.findItem(R.id.action_search);
    View actionView = menuItem.getActionView();

    // We then get the edit text view that is part of the action view
    if (actionView != null) {
        etActionSearch = (AutoCompleteTextView) actionView.findViewById(R.id.myActionEditText);

        if (etActionSearch != null) {
            // We set a listener that will be called when the return/enter key is pressed
            etActionSearch.setOnEditorActionListener(this);

        }

菜单

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_search"
    android:icon="@drawable/action_search"
    android:title="Search"
    app:showAsAction="ifRoom|collapseActionView"
    android:actionLayout="@layout/action_search_singlerow" />
<item
    android:id="@+id/action_changeview"
    android:title="Change View"
    android:icon="@drawable/action_changeviewlist"
    app:showAsAction="ifRoom|collapseActionView"
    />
<item
    android:id="@+id/action_refresh"
    android:icon="@drawable/action_refresh"
    app:showAsAction="ifRoom|collapseActionView"
    android:title="Refresh"/>
<item
    android:id="@+id/action_sorting"
    android:title="Sort"
    android:icon="@drawable/action_sorting"
    app:showAsAction="ifRoom|collapseActionView"
    />

异常

android.content.res.Resources$NotFoundException: Resource ID #0x7f0e0002
        at android.content.res.Resources.getValue(Resources.java:1143)
        at android.content.res.Resources.loadXmlResourceParser(Resources.java:2385)
        at android.content.res.Resources.getLayout(Resources.java:959)
        at android.view.MenuInflater.inflate(MenuInflater.java:107)
        at com.test.test.MainActivity.onCreateOptionsMenu(MainActivity.java:238)
        at android.app.Activity.onCreatePanelMenu(Activity.java:2571)
        at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:451)
        at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:826)
        at com.android.internal.policy.impl.PhoneWindow.run(PhoneWindow.java:236)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5307)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:831)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647)
        at dalvik.system.NativeStart.main(Native Method)

原来 OP 在菜单布局文件中使用了 XML 布局,该布局仅在 layout-port 文件夹中定义,因此崩溃。他添加了相同布局的 layout-land 版本并解决了这个问题。