NPE On AlertDialog

时间:2014-12-02 19:03:47

标签: java android nullpointerexception android-alertdialog

当我尝试按下AlertDialog的按钮时,我的应用程序崩溃了。

public void lalalal(){
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());

    // Setting Dialog Title
    alertDialog.setTitle("Confirm Delete...");

    // Setting Dialog Message
    alertDialog.setMessage("Are you sure you want delete this?");

    // Setting Icon to Dialog
    alertDialog.setIcon(R.drawable.ic_ic);

    // Setting Positive "Yes" Button
    alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int which) {

            // Write your code here to invoke YES event
            Toast.makeText(getActivity(), "You clicked on YES", Toast.LENGTH_SHORT).show();
        }
    });

    // Setting Negative "NO" Button
    alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // Write your code here to invoke NO event
            Toast.makeText(getActivity(), "You clicked on NO", Toast.LENGTH_SHORT).show();
            dialog.cancel();
        }
    });

    // Showing Alert Message
    alertDialog.show();

}

我对于为什么会发生这种情况感到困惑。!..一直在看这个......

至于错误记录:

  java.lang.NullPointerException
        at android.widget.Toast.<init>(Toast.java:105)
        at android.widget.Toast.makeText(Toast.java:261)
        at android.android.startingpoint.dp.test.ListHistoryFragment$2.onClick(ListHistoryFragment.java:138)
        at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:185)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5419)
        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:1046)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
        at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

我的猜测是,当您显示Dialog时,您的Fragment会与Activity分离,这意味着getActivity()将返回null。您可能需要查看Fragment lifecycle,以了解如何将这些内容附加到父内容并将其分离。

解决此问题的一种方式是使用Context维护的Dialog引用:

alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog,int which) {
        Toast.makeText(((Dialog) dialogInterface).getContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
    }
});

// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(((Dialog) dialogInterface).getContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
        dialog.cancel();
    }
});

答案 1 :(得分:-1)

我在grepcode中查看了Toast的某个版本,并猜测您getActivity()会返回null

您致电makeToast(),调用Toast()的构造函数。构造函数的第二行如下所示:

    mTN.mY 
=context.getResources().getDimensionPixelSize(com.android.internal.R.dimen.toast_y_offset);

如果context(本例中的活动)为null,肯定会产生NPE。