Android,OptionsMenu ResourceNotFoundException;切换到横向时

时间:2015-04-18 22:01:11

标签: android switch-statement screen optionmenu

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

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

这是我的代码

@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$1.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)

1 个答案:

答案 0 :(得分:1)

事实证明OP在菜单布局文件中使用了XML布局,该布局文件仅在layout-port文件夹中定义,因此崩溃。他添加了相同布局的布局版本,并修复了问题。