0

嗨,我正在编写一个对话框片段并尝试使用 getArguments 和 setArguments 传递一些数据,但是在使用 getArguments 检索数据时,我得到了一个 NPE。

public class EditNameDialog extends DialogFragment{


public static EditNameDialog newInstance(int type){

    EditNameDialog thedialog=new EditNameDialog();
    Bundle bundle=new Bundle();
    Log.v("typ",type+"");//-->logs fine here
    bundle.putInt("mytype", type);
    thedialog.setArguments(bundle);
    return thedialog;

}



public Dialog onCreateDialog(Bundle icic){

    String ok="Play";
 //I get a NPE here in the below line
    switch(getArguments().getInt("mytype")){ 

    case 1:
    case 2:
        ok="Play";
        break;
    case 3:
        ok="Show";
        break;
    case 4:
        ok="Show";
        break;

    }
    //show alert dialog etc etc here

}

 }

有人可以看看这个并告诉我我会在这里错过什么。

编辑:-这就是我调用对话框片段的方式:-

 DialogFragment newFragment = EditNameDialog.newInstance(1);
 newFragment.show(getSupportFragmentManager(), "mydialog");

LogCat 如下所示:-

01-14 12:52:58.557: E/AndroidRuntime(17271): FATAL EXCEPTION: main
01-14 12:52:58.557: E/AndroidRuntime(17271): java.lang.NullPointerException
01-14 12:52:58.557: E/AndroidRuntime(17271):    at com.civil.dialogs.EditNameDialog.onCreateDialog(EditNameDialog.java:79)
01-14 12:52:58.557: E/AndroidRuntime(17271):    at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:295)
01-14 12:52:58.557: E/AndroidRuntime(17271):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
01-14 12:52:58.557: E/AndroidRuntime(17271):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
01-14 12:52:58.557: E/AndroidRuntime(17271):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-14 12:52:58.557: E/AndroidRuntime(17271):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
01-14 12:52:58.557: E/AndroidRuntime(17271):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
01-14 12:52:58.557: E/AndroidRuntime(17271):    at android.os.Handler.handleCallback(Handler.java:725)
01-14 12:52:58.557: E/AndroidRuntime(17271):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-14 12:52:58.557: E/AndroidRuntime(17271):    at android.os.Looper.loop(Looper.java:137)
01-14 12:52:58.557: E/AndroidRuntime(17271):    at android.app.ActivityThread.main(ActivityThread.java:5039)
01-14 12:52:58.557: E/AndroidRuntime(17271):    at java.lang.reflect.Method.invokeNative(Native Method)
01-14 12:52:58.557: E/AndroidRuntime(17271):    at java.lang.reflect.Method.invoke(Method.java:511)
01-14 12:52:58.557: E/AndroidRuntime(17271):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-14 12:52:58.557: E/AndroidRuntime(17271):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-14 12:52:58.557: E/AndroidRuntime(17271):    at dalvik.system.NativeStart.main(Native Method)
4

0 回答 0