Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application crash when pressing the menu button

Tags:

android

crash

I am trying to create an app for android and I came across the following problem:

The application crashes in a specific phone when I press the menu button. Let me give you some details first.

  • The bug occurs to ONLY on LG Optimus L3 II e430 with Android 4.1.2 (tested on four other phones so far)
  • The application starts with a splash screen and no action bar. At this point menu button just doesn't do anything.
  • With a simple touch we get past the splash screen and we go to the Main Activity which implements ActionBar activity and has a navigation drawer.
  • From this point and after, every time I try to click on the menu button the app crashes.

Here is the layout of the menu and the onCreateOptionsMenu function:

res/menu/main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never" />
</menu>

Part from MainActivity.java

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.main, menu);
            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

Please note that this code is generated from Android Studio.

So far what I've tried:

  • Tried to look at the file that has the problem from the sdk sources (API Level 16 and 21) but they were not relevant to the stack trace (line shown in the stack trace pointed in a location that didn't make sense).
  • Tried to install XPosed fix for Google PlayStore crash with menu button bug. Nothing here either.
  • Found a similar bug report to firefox's bugtracking system so I tried to install Firefox and see if it crashes on my phone when I press Menu Button; firefox didn't crash. (Link to firefox's bug)

Here is the stack trace from LogCat:

10-24 09:08:02.710    4712-4712/com.scaryboxstudios.unrealestateapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.android.internal.policy.impl.PhoneWindow.onKeyUpPanel(PhoneWindow.java:1004)
            at com.android.internal.policy.impl.PhoneWindow.onKeyUp(PhoneWindow.java:1712)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2125)
            at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3611)
            at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3581)
            at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2831)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4929)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
            at dalvik.system.NativeStart.main(Native Method)
like image 260
TheCrafter Avatar asked Oct 24 '14 09:10

TheCrafter


1 Answers

Update: With Appcompat-v7 version 22.0.0, onKeyUp does not seem to fire for the Menu key. The original bug appears to be fixed, so I will likely remove the submenu workaround. Unfortunately I haven't verified the fix on an affected LG 4.1 device.


I ended up doing a workaround for this, which users are reporting has fixed the issue for them.

Implement submenus instead of relying on the overflow menu. The caveat to this is that now every device will see the overflow button in the Action Bar even if they have a Menu key.

The following technique is from https://stackoverflow.com/a/18530179/57490

  1. Convert all overflow options menu items to submenus.

  2. Override onKeyUp in your Activities, have it call Menu.performIdentifierAction(R.id.menu_overflow, 0); and do not call super.onKeyUp for keyCode == KEYCODE_MENU.

like image 190
TalkLittle Avatar answered Nov 01 '22 22:11

TalkLittle