gpt4 book ai didi

java - Dialogfragment onCreateDialog NullPointerException

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:07:31 26 4
gpt4 key购买 nike

我正在尝试加载 DialogFragment,但我有时会在 onCreateDialog 上得到 null。我不确定它什么时候发生,但不是很少见。我该如何解决这个问题?

实用工具.java

    currentDialog = PopupDialog.newInstance(type, title, maString, isCancelable);
currentDialog.show(ft, "dialog");

弹出对话框.java

public class PopupDialog extends DialogFragment{


public static final int POPUP_ERROR = 0;
public static final int POPUP_WARNNING = 1;

private int type;
private String title;
private String messageString;
private boolean isCancelable;

public static PopupDialog newInstance(int type,String title,String maString,boolean isCancelable) {
PopupDialog f = new PopupDialog();

Bundle args = new Bundle();
args.putInt("type",type);
args.putString("title", title);
args.putString("maString", maString);
args.putBoolean("isCancelable", isCancelable);
f.setArguments(args);

return f;
}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle arg = getArguments();
type = arg.getInt("type");
title = arg.getString("title");
messageString = arg.getString("maString");
isCancelable = arg.getBoolean("isCancelable");
setStyle(DialogFragment.STYLE_NO_TITLE,0);
if (!isCancelable){
setCancelable(false);
}

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.popup_error, container, false);

TextView popupTitle = (TextView) v.findViewById(R.id.popupTitle);
popupTitle.setText(title);

TextView popupMessage = (TextView) v.findViewById(R.id.popupMessage);
popupMessage.setText(messageString);

ImageView popupIcon = (ImageView) v.findViewById(R.id.popupIcon);
messageString = messageString + "\n";
if(type == POPUP_ERROR){
messageString = messageString + getString(R.string.error_default_ext);
popupIcon.setImageResource(R.drawable.icon_error);
}
else{
popupIcon.setImageResource(R.drawable.warning_icon);
}

popupMessage.setText(messageString);


Button okButton = (Button) v.findViewById(R.id.okButton);

okButton.setOnClickListener(new dismissDialogOnClick());

if (!isCancelable){
okButton.setVisibility(View.GONE);
}
return v;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog d = super.onCreateDialog(savedInstanceState);
d.setCanceledOnTouchOutside(false);
return d;
}

编辑:

附上堆栈跟踪。如果我尝试在 getDialog onCreateDialog 上执行操作(就像第一种情况,它并不总是发生

java.lang.NullPointerException
at android.app.DialogFragment.onActivityCreated(DialogFragment.java:469)
at android.app.Fragment.performActivityCreated(Fragment.java:1703)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
at android.app.BackStackRecord.run(BackStackRecord.java:682)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

getDialog().setCanceledOnTouchOutside(false);

getDialog 在调用内部显示后返回 Dialog 对象。在 onCreateView 中调用它还为时过早。您可以重写onCreateDialog,获取父类(super class)返回的对象,并调用该对象的方法

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog d = super.onCreateDialog(savedInstanceState);
d.setCanceledOnTouchOutside(false);
return d;
}

关于java - Dialogfragment onCreateDialog NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28396390/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com