在我的应用程序中,当我关闭警报对话框然后获取错误java.lang.IllegalStateException。我点击肯定按钮打开对话框片段。但每次都会导致错误。
FATAL EXCEPTION: main
Process: com.icanstudioz.android.njoyful, PID: 18564
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3718)
at android.view.ViewGroup.addView(ViewGroup.java:3564)
at android.view.ViewGroup.addView(ViewGroup.java:3540)
at android.support.v7.app.AlertController.setupView(AlertController.java:444)
at android.support.v7.app.AlertController.installContent(AlertController.java:217)
at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:240)
at android.app.Dialog.dispatchOnCreate(Dialog.java:361)
at android.app.Dialog.show(Dialog.java:262)
at android.support.v4.app.DialogFragment.onStart(DialogFragment.java:399)
at android.support.v4.app.Fragment.performStart(Fragment.java:1813)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:989)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
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:5292)
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:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
我的代码就在这里,
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Select").setView(np);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
currentItem.setItemQuantity(String
.valueOf(np.getValue()));
v.setSelected(true);
if (currentItem.getModifier_group()
.length() == 0) {
NjoyApplication
.getInstance()
.addNewItemWithModifiers(
currentItem, "0", false);
updateCart();
} else {
DialogFragment dialogFrag = new selectModifier();
Bundle b = new Bundle();
b.putString("ID", currentItem.getId()
+ "");
dialogFrag.setArguments(b);
dialogFrag.setTargetFragment(
Create_Order_ListFragment.this,
Modifier.SELECT_NEW_MODIFIER);
b.putInt("REQUEST_CODE",
Modifier.SELECT_NEW_MODIFIER);
dialogFrag.setCancelable(true);
dialogFrag.show(getFragmentManager(),
"dialog");
}
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
v.setSelected(false);
dialog.dismiss();
}
}).show();
见createview(),
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.common_gridview, container,false);
}
答案 0 :(得分:1)
1. if you are using fragment than make sure,
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_reject, container,false);
return view;
}
in above code there should be **false** after container.
2. you must set the content view for the activity only once. **setContentView(XYZ);**
########======#######
try this in oncreatview,
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
parent.removeView(view);
}
}
try {
view = inflater.inflate(R.layout.fragment_main, container, false);
} catch (InflateException e) {
}
答案 1 :(得分:0)
尝试在本地声明dialog
。
java.lang.IllegalStateException:指定的子节点已经有了 家长。您必须首先在孩子的父母身上调用removeView()。
问题是你试图在Activity中连接两次相同的AlertDialog,每次尝试创建一个。
试试这段代码。
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final AlertDialog alertd = builder.create();
builder.setTitle("Select").setView(np);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
currentItem.setItemQuantity(String
.valueOf(np.getValue()));
v.setSelected(true);
if (currentItem.getModifier_group()
.length() == 0) {
NjoyApplication
.getInstance()
.addNewItemWithModifiers(
currentItem, "0", false);
updateCart();
} else {
DialogFragment dialogFrag = new selectModifier();
Bundle b = new Bundle();
b.putString("ID", currentItem.getId()
+ "");
dialogFrag.setArguments(b);
dialogFrag.setTargetFragment(
Create_Order_ListFragment.this,
Modifier.SELECT_NEW_MODIFIER);
b.putInt("REQUEST_CODE",
Modifier.SELECT_NEW_MODIFIER);
dialogFrag.setCancelable(true);
dialogFrag.show(getFragmentManager(),
"dialog");
}
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
v.setSelected(false);
alertd.dismiss();
}
}).show();
我希望它有所帮助!