Application crashes on clicking options

╄→гoц情女王★ 提交于 2019-12-12 04:38:24

问题


So using Android Studio 2.3.1 I have the most simple app possible, using default settings and choosing a Basic App template. The app gets created just fine, but crashes if you try to click the options button at the top right corner - i.e. the three dots. This is a serious problem...

This is the error:

04-30 17:51:13.871 13942-13942/com.topository.optionstest01 W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
04-30 17:51:17.987 13942-13942/com.topository.optionstest01 D/AndroidRuntime: Shutting down VM
04-30 17:51:17.988 13942-13942/com.topository.optionstest01 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.topository.optionstest01, PID: 13942
                                                                              java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                                  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:751)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6121)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

Here is my gradle.build file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "com.topository.optionstest01"
        minSdkVersion 23
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:25.3.1'
    testCompile 'junit:junit:4.12'
}

For completeness:

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Here is my menu.xml:

<?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">
    <item
        android:id="@+id/settings"
        android:orderInCategory="100"
        android:title="Settings"
        android:titleCondensed="The settings"
        app:showAsAction="never"/>

</menu>

And the device is a Pixel running 7.1.2

Has anyone else come across this? Any ideas?

Thanks


回答1:


I have had the same issue, my app was crashing, and the example basic app from Android Studio was crashing on clicking "options"menu button (with three dots), too.

Crash log:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 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:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Setup: Android Studio 2.3.1 Gradle 3.3

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.



来源:https://stackoverflow.com/questions/43704306/application-crashes-on-clicking-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!