我在从PreferenceActivity实例化的Dialog中出现此错误。有人能告诉我出了什么问题吗?
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;
}
}
这就是stacktrace
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
,获取picker
和dialogView
的父项并检查childCount
。没运气。还有其他建议吗?
答案 0 :(得分:1)
视图只能有一个父级。 onCreateDialogView
将返回dialogView
,然后将其添加到视图中(从而接收父级)。如果第二次调用onCreateDialogView
,则dialogView
将拥有父级,并将抛出IllegalStateException
。相反,请在dialogView
内初始化picker
和onCreateDialogView
,以便每次都以新的无父级视图开始。