我的应用使用导航抽屉。我的应用程序将崩溃并出现以下错误:
08-30 16:44:30.079 22176-22176/com.imincode.meniti W/System.err﹕ java.lang.NullPointerException
08-30 16:44:30.089 22176-22176/com.imincode.meniti W/System.err﹕ at com.imincode.meniti.MenuGoals$2$1.onClick(MenuGoals.java:127)
08-30 16:44:30.089 22176-22176/com.imincode.meniti W/System.err﹕ at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
08-30 16:44:30.089 22176-22176/com.imincode.meniti W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 16:44:30.089 22176-22176/com.imincode.meniti W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
第127行显示在
下面@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
MenuItem tbAddItem = menu.findItem(R.id.tbAddItem);
tbAddItem.setVisible(true);
tbAddItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
new AlertDialog.Builder(getActivity())
.setTitle(getContext().getString(R.string.download_paid_title))
.setMessage(getContext().getString(R.string.download_paid_content))
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Uri uri = Uri.parse("market://details?id=com.imincode.menitiplus");
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
try {
startActivity(goToMarket); //line 127
} catch (ActivityNotFoundException e) {
getActivity().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + getActivity().getPackageName())));
} }
})
.setNegativeButton(android.R.string.no, null).show();
return false;
}
});
请注意,只有在满足以下条件时才会抛出错误:
请注意,Play商店可以在没有任何问题或错误的情况下启动,如果我没有在打开片段之前加载带有admob的片段,该片段有一个按钮来加载Play商店(并且最后点击按钮加载Play商店)。
顺便说一句,这是在另一个片段上加载adMob的代码。
AdView mAdView = (AdView) rootView.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
mAdView.destroy();
答案 0 :(得分:0)
经过几个小时的调试,发现只需通过更改返回false在AlertDialog中返回true即可解决问题。现在一切都很好。
基本上这是新代码:
new AlertDialog.Builder(getContext())
.setTitle(getContext().getString(R.string.download_paid_title))
.setMessage(getContext().getString(R.string.download_paid_content))
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Uri uri = Uri.parse("market://details?id=com.imincode.menitiplus");
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
try {
startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + getActivity().getPackageName())));
}
}
})
.setNegativeButton(android.R.string.no, null).show();
return true;
如您所见,最后一次返回从false更改为true。不知道为什么会这样。