1

我在这里尝试谷歌给出的代码,我得到一个 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();
}}
4

2 回答 2

2

改变

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

mDialogFragment dialog = new mDialogFragment();
dialog.show(getFragmentManager(), "mDialogFragment");
于 2013-07-11T16:01:32.617 回答
1

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

Fragment 尚未附加到 Activity

为了解决这个问题,您必须按照建议等待 Activity 准备好,或者在您的 Dialog 可用时自行传递上下文。

于 2013-07-11T16:03:27.593 回答