DialogFragment给出NullPointerException

时间:2013-07-11 15:56:15

标签: android nullpointerexception android-dialogfragment

我正在尝试由Google here提供的代码,我收到NullPointerException,因为getActivity()中的mDialogFragment返回null。我不明白为什么。有人可以解释错误吗?

错误记录

07-11 21:12:59.240: E/AndroidRuntime(1968): FATAL EXCEPTION: main
07-11 21:12:59.240: E/AndroidRuntime(1968): java.lang.NullPointerException
07-11 21:12:59.240: E/AndroidRuntime(1968): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
07-11 21:12:59.240: E/AndroidRuntime(1968): at android.app.AlertDialog$Builder.<init>(AlertDialog.java:359)
07-11 21:12:59.240: E/AndroidRuntime(1968): at com.vinit.android.market.licensing.mDialogFragment.onCreateDialog(mDialogFragment.java:18)
07-11 21:12:59.240: E/AndroidRuntime(1968): at com.vinit.android.market.licensing.MainActivity$1.onClick(MainActivity.java:90)
07-11 21:12:59.240: E/AndroidRuntime(1968): at android.view.View.performClick(View.java:3511)
07-11 21:12:59.240: E/AndroidRuntime(1968): at android.view.View$PerformClick.run(View.java:14105)
07-11 21:12:59.240: E/AndroidRuntime(1968): at android.os.Handler.handleCallback(Handler.java:605)
07-11 21:12:59.240: E/AndroidRuntime(1968): at android.os.Handler.dispatchMessage(Handler.java:92)
07-11 21:12:59.240: E/AndroidRuntime(1968): at android.os.Looper.loop(Looper.java:137)
07-11 21:12:59.240: E/AndroidRuntime(1968): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-11 21:12:59.240: E/AndroidRuntime(1968): at java.lang.reflect.Method.invokeNative(Native Method)
07-11 21:12:59.240: E/AndroidRuntime(1968): at java.lang.reflect.Method.invoke(Method.java:511)
07-11 21:12:59.240: E/AndroidRuntime(1968): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-11 21:12:59.240: E/AndroidRuntime(1968): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-11 21:12:59.240: E/AndroidRuntime(1968): at dalvik.system.NativeStart.main(Native Method)

MainActivity.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mStatusText = (TextView) findViewById(R.id.status_text);
    mCheckLicenseButton = (Button) findViewById(R.id.check_license_button);
    mCheckLicenseButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {                
       (new mDialogFragment()).onCreateDialog(null).show();   //this line giving error
        }
    });

}

mDialogFragment.java

public class mDialogFragment extends DialogFragment{

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    AlertDialog.Builder builder=new AlertDialog.Builder(getActivity()); //this line giving error
    builder.setMessage(R.string.Testing).setPositiveButton(R.string.Buy, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

        }
    }).setNegativeButton(R.string.quit_button,new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

        }
    });
    return builder.create();
}}

2 个答案:

答案 0 :(得分:2)

更改

   (new mDialogFragment()).onCreateDialog(null).show();   //this line giving error

mDialogFragment dialog = new mDialogFragment();
dialog.show(getFragmentManager(), "mDialogFragment");

答案 1 :(得分:1)

根据使用方式的不同,这可以解决您的问题。

Fragment has not attached to Activity yet

为了解决这个问题,你必须按照建议等待活动准备好,或者在可用于你的对话框时自己传递上下文。