我需要将一个ArrayList从一个Activity传递给一个CustomDialog,但是我得到了Nullpointer异常,我知道我肯定做错了什么,我需要一些指导。这是我的代码。请帮忙!
或许有更好的方法我需要实现将ArrayList传递给CustomDialog,你可以请指导我。感谢
MainActivity
public class MainActivity extends AppCompatActivity {
/** List **/
private ArrayList<String> groupList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
groupList = new ArrayList<String>();
groupList.add("One");
groupList.add("Two");
groupList.add("Three");
groupList.add("Four");
CustomDialog customdialog = new CustomDialog();
customdialog.show(getFragmentManager(), "My Alert");
// Supply input as an argument.
if(groupList.size()!=0) {
Bundle args = new Bundle();
args.putSerializable("groupList", groupList);
customdialog.setArguments(args);
}
}
}
CustomDialog
public class CustomDialog extends DialogFragment{
ArrayList<String> groupList;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int radiochoice=0;
Bundle extras = getActivity().getIntent().getExtras();
if (extras != null) {
groupList = (ArrayList<String>) getActivity().getIntent().getSerializableExtra("groupList");
if(groupList.size()!=0) {
Log.d("groupList size:", Integer.toString(groupList.size()));
}
}
/** The rest of the Custom dialog code under here **/
}
}
这是我在下面的堆栈跟踪
Process: com.example.practice, PID: 9203
java.lang.NullPointerException
at com.example.practice.dialog.CustomDialog.onCreateDialog(CustomDialog.java:34)
at android.app.DialogFragment.getLayoutInflater(DialogFragment.java:398)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:698)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5323)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)