gpt4 book ai didi

java - 框架布局非法状态异常 : The specified child already has a parent

转载 作者:行者123 更新时间:2023-11-30 02:12:57 24 4
gpt4 key购买 nike

我在从 PreferenceActivity 实例化的对话框中遇到此错误。谁能告诉我哪里出了问题?

public class NumberPickerPreference extends DialogPreference {

public static final int MAX_VALUE = 24;
public static final int MIN_VALUE = 2;

public NumberPicker picker;
FrameLayout dialogView;
private int value;
Context ctx;


public NumberPickerPreference(Context context, AttributeSet attrs) {
super(context, attrs);
ctx = context;
dialogView = new FrameLayout(ctx);
picker = new NumberPicker(ctx);
}

public NumberPickerPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

}

@Override
protected View onCreateDialogView() {
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
picker.setLayoutParams(layoutParams);
dialogView.addView(picker); // <- this line has the error

return dialogView;
}

@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
dialog.dismiss();
}

@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
picker.setMinValue(MIN_VALUE);
picker.setMaxValue(MAX_VALUE);
picker.setValue(getValue());
}

@Override
protected void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
setValue(picker.getValue());
}
}

@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getInt(index, MIN_VALUE);
}

@Override
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
setValue(restorePersistedValue ? getPersistedInt(MIN_VALUE) : (Integer) defaultValue);
}

public void setValue(int value) {
this.value = value;
persistInt(this.value);
}

public int getValue() {
return this.value;
}
}

这是堆栈跟踪

04-26 00:47:30.870  25385-25385/tk.stayhappy.packagetracker E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: tk.stayhappy.packagetracker, PID: 25385
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:3937)
at android.view.ViewGroup.addView(ViewGroup.java:3787)
at android.view.ViewGroup.addView(ViewGroup.java:3759)
at com.android.internal.app.AlertController.setupView(AlertController.java:492)
at com.android.internal.app.AlertController.installContent(AlertController.java:236)
at android.app.AlertDialog.onCreate(AlertDialog.java:356)
at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
at android.app.Dialog.show(Dialog.java:274)
at android.preference.DialogPreference.showDialog(DialogPreference.java:294)
at android.preference.DialogPreference.onClick(DialogPreference.java:274)
at android.preference.Preference.performClick(Preference.java:983)
at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:214)
at android.widget.AdapterView.performItemClick(AdapterView.java:305)
at android.widget.AbsListView.performItemClick(AbsListView.java:1148)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3059)
at android.widget.AbsListView$3.run(AbsListView.java:3866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

我没有更多的想法,因为它可能是错的。尝试使用 ApplicationContext 更改上下文,在关闭对话框时删除 picker,获取 pickerdialogView 的父级并检查 childCount。没有运气。还有其他建议吗?

最佳答案

一个 View 只能有一个父 View 。 onCreateDialogView 将返回 dialogView,然后将其添加到 View (并因此接收父 View )。如果 onCreateDialogView 被第二次调用,dialogView 将已经有一个父级并且会抛出一个 IllegalStateException。相反,在 onCreateDialogView 中初始化 dialogViewpicker,这样您每次都可以从新鲜的、无父 View 开始。

关于java - 框架布局非法状态异常 : The specified child already has a parent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29871199/

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