创建 DialogFragment 时出现 NullPointerException
NullPointerException when creating DialogFragment
我正在将数据从 main activity 传递到 onCreateDialog 中 DialogFragment 的 EditListNameDialogFragment 超级构造函数,抛出这些异常。
Process: com.example.zar.shoppinglistplusfirebase, PID: 32464
java.lang.NullPointerException
at com.example.zar.shoppinglistplusfirebase.ui.activeListDetails.EditListDialogFragment.createDialogHepler(EditListDialogFragment.java:67)
at com.example.zar.shoppinglistplusfirebase.ui.activeListDetails.EditListNameDialogFragment.onCreateDialog(EditListNameDialogFragment.java:47)
at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:312)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1298)
at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1998)
at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:709)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5590)
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:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
来自 EditListNameDialogFragment 的 onCreateDialogFragment
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.createDialogHelper(R.string.positive_button_edit_item);
helpSetDefaultValueEditText(mListName);
return dialog;
}
super.createDialogHelper(String r) 来自 EditListDialogFragment
protected Dialog createDialogHepler(int stringResourceforPositiveButton){
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity(),R.style.CustomTheme_Dialog);
LayoutInflater inflater=getActivity().getLayoutInflater();
View rootView=inflater.inflate(mResource,null);
mEditTextForList=(EditText) rootView.findViewById(R.id.edit_text_list_dialog);
mEditTextForList.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if (i== EditorInfo.IME_ACTION_DONE || keyEvent.getAction()==KeyEvent.ACTION_DOWN){
doListEdit();
EditListDialogFragment.this.getDialog().cancel();
}
return true;
}
});
builder.setView(rootView).setPositiveButton(stringResourceforPositiveButton, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
doListEdit();
EditListDialogFragment.this.getDialog().cancel();
}
})
.setNegativeButton(R.string.negative_button_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
EditListDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
我认为那是因为您没有正确初始化片段。
您不应该使用自定义方法来初始化 DialogFragment,最好这样做:
public class MyDialogFragment extends DialogFragment {
private textView myTextView;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View rootView = inflater.inflate(R.layout.dialog_list, null);
// textView = (TextView) ...
// Set layout for this dialog
builder.setView(rootView)
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the positive button event back to the host fragment
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
为了获得调用组件的句柄以便在按钮上的操作已执行时进行通信,您应该使用回调。
参见 docs
错误是 EditListDialogFragment 的 xml 不包含调用 super.createDialogHelper(String r) 的 EditText 字段,我为它包含了 EditText 字段,问题随之解决。
我正在将数据从 main activity 传递到 onCreateDialog 中 DialogFragment 的 EditListNameDialogFragment 超级构造函数,抛出这些异常。
Process: com.example.zar.shoppinglistplusfirebase, PID: 32464 java.lang.NullPointerException at com.example.zar.shoppinglistplusfirebase.ui.activeListDetails.EditListDialogFragment.createDialogHepler(EditListDialogFragment.java:67) at com.example.zar.shoppinglistplusfirebase.ui.activeListDetails.EditListNameDialogFragment.onCreateDialog(EditListNameDialogFragment.java:47) at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:312) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1298) at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136) at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1998) at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:709) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5590) 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:1268) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) at dalvik.system.NativeStart.main(Native Method)
来自 EditListNameDialogFragment 的 onCreateDialogFragment
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.createDialogHelper(R.string.positive_button_edit_item);
helpSetDefaultValueEditText(mListName);
return dialog;
}
super.createDialogHelper(String r) 来自 EditListDialogFragment
protected Dialog createDialogHepler(int stringResourceforPositiveButton){
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity(),R.style.CustomTheme_Dialog);
LayoutInflater inflater=getActivity().getLayoutInflater();
View rootView=inflater.inflate(mResource,null);
mEditTextForList=(EditText) rootView.findViewById(R.id.edit_text_list_dialog);
mEditTextForList.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if (i== EditorInfo.IME_ACTION_DONE || keyEvent.getAction()==KeyEvent.ACTION_DOWN){
doListEdit();
EditListDialogFragment.this.getDialog().cancel();
}
return true;
}
});
builder.setView(rootView).setPositiveButton(stringResourceforPositiveButton, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
doListEdit();
EditListDialogFragment.this.getDialog().cancel();
}
})
.setNegativeButton(R.string.negative_button_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
EditListDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
我认为那是因为您没有正确初始化片段。 您不应该使用自定义方法来初始化 DialogFragment,最好这样做:
public class MyDialogFragment extends DialogFragment {
private textView myTextView;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View rootView = inflater.inflate(R.layout.dialog_list, null);
// textView = (TextView) ...
// Set layout for this dialog
builder.setView(rootView)
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the positive button event back to the host fragment
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
为了获得调用组件的句柄以便在按钮上的操作已执行时进行通信,您应该使用回调。 参见 docs
错误是 EditListDialogFragment 的 xml 不包含调用 super.createDialogHelper(String r) 的 EditText 字段,我为它包含了 EditText 字段,问题随之解决。