应用程序在单击选项时崩溃

时间:2017-04-30 08:10:51

标签: android crash settings options

因此,使用Android Studio 2.3.1我可以使用最简单的应用程序,使用默认设置并选择基本应用程序模板。应用程序创建得很好,但如果您尝试单击右上角的选项按钮(即三个点),则会崩溃。这是一个严重的问题......

这是错误:

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)

这是我的gradle.build文件:

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'
}

为了完整性:

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);
    }
}

这是我的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>

该设备是运行7.1.2的Pixel

还有其他人遇到过这个吗?有什么想法吗?

由于

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我的应用程序崩溃了,Android Studio中的示例基本应用程序也在单击“选项”菜单按钮(带有三个点)时崩溃。

崩溃日志:

  

java.lang.NullPointerException:尝试调用虚方法'void   android.widget.TextView.setText(java.lang.CharSequence)'为null   对象参考                                                                                  在   android.support.v7.view.menu.ListMenuItemView.setTitle(ListMenuItemView.java:127)                                                                                  在   android.support.v7.view.menu.ListMenuItemView.initialize(ListMenuItemView.java:113)                                                                                  在   android.support.v7.view.menu.MenuAdapter.getView(MenuAdapter.java:100)                                                                                  在   android.support.v7.view.menu.MenuPopup.measureIndividualMenuWidth(MenuPopup.java:160)                                                                                  在   android.support.v7.view.menu.StandardMenuPopup.tryShow(StandardMenuPopup.java:153)                                                                                  在   android.support.v7.view.menu.StandardMenuPopup.show(StandardMenuPopup.java:187)                                                                                  在   android.support.v7.view.menu.MenuPopupHelper.showPopup(MenuPopupHelper.java:290)                                                                                  在   android.support.v7.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:175)                                                                                  在   android.support.v7.widget.ActionMenuPresenter $ OpenOverflowRunnable.run(ActionMenuPresenter.java:803)                                                                                  在android.os.Handler.handleCallback(Handler.java:739)                                                                                  在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                  在android.os.Looper.loop(Looper.java:148)                                                                                  在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                  at java.lang.reflect.Method.invoke(Native Method)                                                                                  在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

设置:Android Studio 2.3.1 Gradle 3.3

我找到的解决方案降级 Gradle插件从 3.3降至2.14.1

1)替换插件版本 顶级 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'
    ...
    }

如果使用包装器 2)删除/gradle/wrapper/gradle-wrapper.jar

3)更换内容 的 /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

建设 - &GT;清洁项目 - &gt;重建项目 - &gt;当获得三点式右上角菜单时,崩溃已经消失。