问题描述
正常按钮可以正常工作,但是单击三点按钮会出现例外情况
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;
}
我的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"
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>
日志
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)
点击监听器
@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);
}
}
单击菜单溢出会导致npe(在这种情况下为action_main_top).
单击常规菜单(刷新和搜索)效果很好.
clicking on the menu overflow causes npe (in this case, the action_main_top).
clicking on the normal menu (refresh & search) works fine.
推荐答案
在进行了详尽的研究之后,我在这里找到了这个答案:
after doing an exhaustive research, I found this answer here: App Crashes on Clicking Options
该答案解决了我的问题,显然,该问题仅适用于Gradle 2.3.3版,您需要降级到2.2.3才能对其进行修复. (希望很快会解决此错误以进行升级)
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)
在这里,我粘贴 Alex Bouroff
我发现的解决方案是将Gradle插件从3.3降级到2.14.1
The solution that I have found was downgrading Gradle plugin from 3.3 down to 2.14.1
1)替换顶级build.gradle中的插件版本:
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'
...
}
如果使用包装器2)删除/gradle/wrapper/gradle-wrapper.jar
If you use a wrapper 2) delete the /gradle/wrapper/gradle-wrapper.jar
3)替换/gradle/wrapper/gradle-wrapper.properties的内容
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->清洁项目->重建项目->取消右上角三点菜单时的崩溃.
build-> clean project -> rebuild project -> the crash when acessing three-dot upper-right corner menu had gone.
这篇关于单击三个点时,Android溢出菜单nullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!