1

ここでGoogleから提供されたコードを試していましたが、 getActivity()inmDialogFragmentがnullを返しているため、NullPointerExceptionが発生しています。理由がわかりません。誰かがエラーを説明できますか?

エラーログ

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 に答える