Search code examples
androidswitch-statementscreenoptionmenu

Android,OptionMenu ResourceNotFoundException; when switch to landspace


When my android app started in portrait screen, there are optionmenus in actionbar; but when switch to landspace, I'm getting resource not found exception. Exception on inflater.inflate(R.menu.main, menu);

Why I'm getting this error, Thanks

And here my code

@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

<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"
    />

Exception

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)

Solution

  • It turns out OP was using a XML layout in the menu layout file that was only defined in the layout-port folder, hence the crash. He added a layout-land version of the same layout and he fixed the issue.