10

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)
TheCrafter
  • 1,909
  • 2
  • 23
  • 44

3 Answers3

2

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.

Community
  • 1
  • 1
TalkLittle
  • 8,866
  • 6
  • 54
  • 51
  • Thanks for your answer TalkLittle. What you said is true of course, but I do not need a workaround. In fact, overriding onKeyUp is the first thing I thought. The main problem is to find the root of this problem and a correct way to fix it. Not a way to avoid it!! – TheCrafter Nov 07 '14 at 22:38
0

After stumbling upon the same problem recently I found the root of the problem. The problem is compatibility issues between older and newer support libraries. It seems that I used depreciated stuff around my code together with newer stuff.

I am sorry for being kind of abstract but this question is 4 months old and I cannot remember what exactly were the incorrect lines of code. If memory serves right, the problem lied upon auto generated methods from Android Studio for application drawer activities. I used the Drawer Application project template from Android Studio and I chose to support very old Android APIs too so Android Studio chose the depreciated Android Support Library.

The point is that I resolved the problem when I refactored the code to use non depreciated techniques only.

If you are fighting against a similar problem I strongly recommend remove everything that Android Studio (I assume that you use Android Studio or Eclipse) marks as depreciated.

TheCrafter
  • 1,909
  • 2
  • 23
  • 44
0

Also for catching Menu button can use next:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_MENU) {


        // TODO - Your user code

        /*
        if (event.getAction() == KeyEvent.ACTION_DOWN
                && event.getRepeatCount() == 0) {

            // Tell the framework to start tracking this event.
            //getKeyDispatcherState().startTracking(event, this);
            return true;

        } else if (event.getAction() == KeyEvent.ACTION_UP) {

           // getKeyDispatcherState().handleUpEvent(event);
            if (event.isTracking() && !event.isCanceled()) {

                // DO BACK ACTION HERE
                return true;

            }
        }*/

        // if you don't want use system  listener 
        // return super.dispatchKeyEvent(event);
        return false;

    } else {
        return super.dispatchKeyEvent(event);
    }
}

Worked for lastest AppCompat and SDK version - 22.0

kaftanati
  • 480
  • 4
  • 5
  • Yeah that will work but it counts as a "workaround". As I stated in TalkLittle's answer below, I have found many workarounds. But I was curious for the actual solution and/or root of the problem. Thanks for your answer though! – TheCrafter Mar 29 '15 at 10:35