Android overflow menu nullPointerException when cl

2019-09-16 11:25发布

The normal button works fine, but clicking the three dots button makes an exception

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}

my xml file

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">


    <item
        android:icon="@drawable/refresh"
        android:id="@+id/action_main_refresh"
        app:showAsAction="always"
        android:title="@string/refresh" />

    <item
        android:id="@+id/action_main_search"
        android:title="@string/search"
        android:icon="@drawable/seach"
        app:showAsAction="always"/>

    <item
        android:id="@+id/action_main_top"
        android:title="@string/sort_top"
        app:showAsAction="never" />


</menu>

Logs

06-22 23:12:49.481 1289-1289/? E/AndroidRuntime: FATAL EXCEPTION: main
                                             java.lang.NullPointerException
                                                 at android.support.v7.view.menu.ListMenuItemView.setTitle(ListMenuItemView.java:127)
                                                 at android.support.v7.view.menu.ListMenuItemView.initialize(ListMenuItemView.java:113)
                                                 at android.support.v7.view.menu.MenuAdapter.getView(MenuAdapter.java:100)
                                                 at android.support.v7.view.menu.MenuPopup.measureIndividualMenuWidth(MenuPopup.java:160)
                                                 at android.support.v7.view.menu.StandardMenuPopup.tryShow(StandardMenuPopup.java:153)
                                                 at android.support.v7.view.menu.StandardMenuPopup.show(StandardMenuPopup.java:187)
                                                 at android.support.v7.view.menu.MenuPopupHelper.showPopup(MenuPopupHelper.java:290)
                                                 at android.support.v7.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:175)
                                                 at android.support.v7.widget.ActionMenuPresenter$OpenOverflowRunnable.run(ActionMenuPresenter.java:803)
                                                 at android.os.Handler.handleCallback(Handler.java:730)
                                                 at android.os.Handler.dispatchMessage(Handler.java:92)
                                                 at android.os.Looper.loop(Looper.java:137)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5103)
                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                 at java.lang.reflect.Method.invoke(Method.java:525)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                 at dalvik.system.NativeStart.main(Native Method)

click listener

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    //Log.i(TAG, "sort URL: " + makeUrlCustom(item.getItemId()));
    switch(item.getItemId()){
        case R.id.action_main_refresh:
            if(!adapter.isEmpty()) adapter.clear();
            noData.setVisibility(View.GONE);
            get(url, null);
            return true;
        case R.id.action_main_search:
            AlertDialog.Builder alert = new AlertDialog.Builder(this);

            final EditText dialogText = new EditText(this);

            alert.setView(dialogText);
            alert.setTitle("Search subreddit");

            alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    url = dialogText.getText().toString();
                    if(!adapter.isEmpty()) adapter.clear();
                    noData.setVisibility(View.GONE);
                    Log.i(TAG, "url from searchText is: " + url);
                    if(url.equals("") || url == null){
                        Toast.makeText(MainActivity.this, "Please insert valid subreddit", Toast.LENGTH_SHORT).show();
                    }else{
                        get(url, null);
                    }
                }
            });

            // create alert dialog
            AlertDialog alertDialog = alert.create();
            // show it
            alertDialog.show();
            return true;
        case R.id.action_main_top:
            Log.i(TAG, "sort top");
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

clicking on the menu overflow causes npe (in this case, the action_main_top).
clicking on the normal menu (refresh & search) works fine.

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-09-16 11:40

after doing an exhaustive research, I found this answer here: App Crashes on Clicking Options

That answer solved my issue, aparently the issue is only for the Gradle version 2.3.3 and you need to downgrade to the 2.2.3 in order to fix it. (Hope that soon fix this bug to upgrade)

Here I paste the solution provided by Alex Bouroff

The solution that I have found was downgrading Gradle plugin from 3.3 down to 2.14.1

1) Replace the plugin version in top-level build.gradle:

dependencies {
    //REPLACED 2.3.3->2.2.3(MENU ISSUE)
    //classpath 'com.android.tools.build:gradle:2.3.3'
    classpath 'com.android.tools.build:gradle:2.2.3'
    ...
    }

If you use a wrapper 2) delete the /gradle/wrapper/gradle-wrapper.jar

3) replace the contents of /gradle/wrapper/gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip

build-> clean project -> rebuild project -> the crash when acessing three-dot upper-right corner menu had gone.

查看更多
登录 后发表回答