我创建了一个自定义对话框首选项,它有两个微调器,以便选择特定的时间间隔(例如 2 秒或 2 分钟或 3 小时等)。因此,当我在首选项屏幕中单击我的自定义首选项时,自定义对话框首选项会弹出起来,它显示了两个微调器。但是,当我选择其中一个微调器(弹出另一个对话框 - 请参阅第二张图像)并且如果我更改屏幕的方向时,应用程序将被强制关闭。:S 这发生在 Android 2.3.6 Gingerbread 中。我已经用 android 4.1 进行了测试,它工作正常,但微调器不同,它不在对话框中,而是在组合框样式中。
我已将所有函数的代码放在 try catch 中,但我无法捕获异常。以下是错误:
01-17 15:14:43.929: E/WindowManager(5029): Activity com.myapp.appdemo.EditPreferences has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405c1b00 that was originally added here
01-17 15:14:43.929: E/WindowManager(5029): android.view.WindowLeaked: Activity com.myapp.appdemo.EditPreferences has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405c1b00 that was originally added here
01-17 15:14:43.929: E/WindowManager(5029): at android.view.ViewRoot.<init>(ViewRoot.java:263)
有一个解决方案可以将 configchanges 更改为此:android:configChanges="orientation|keyboardHidden",但我想知道是否有另一种方法可以解决这个问题,因为我已经阅读了很多关于这个解决方法可能的问题有一些副作用。
我还尝试从 onDismiss 函数的布局中删除微调器,但我没有解决问题。:S 所以使用函数或不使用函数都不能解决问题。
这是自定义 DialogPreference 类:
public class PreferenceCustomTime extends DialogPreference {
private Context context;
private Spinner spinner1, spinner2;
private ArrayAdapter<CharSequence> adapter, adapterValue;
public PreferenceCustomTime(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
setPersistent(false);
setDialogLayoutResource(R.layout.spinner_list_callpoint);
}
@Override
protected void onBindDialogView(View view) {
spinner1 = (Spinner) view.findViewById(R.id.spinner1);
spinner2 = (Spinner) view.findViewById(R.id.spinner2);
adapter = ArrayAdapter.createFromResource(context, R.array.arrayTypeTime, android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //simple_spinner_dropdown_item simple_spinner_item
spinner1.setAdapter(adapter);
spinner1.setSelection(0);
adapterValue = ArrayAdapter.createFromResource(context, R.array.arrayValueTime1, android.R.layout.simple_spinner_dropdown_item);
adapterValue.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapterValue);
super.onBindDialogView(view);
}
@Override
public void onDismiss(DialogInterface dialog) {
LayoutInflater inflater = LayoutInflater.from(context);
LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.spinner_list_callpoint , null);
linearLayout.removeView(spinner1);
linearLayout.removeView(spinner2);
super.onDismiss(dialog);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
SharedPreferences.Editor editor = getEditor();
editor.putString(getKey() + ".type", spinner1.getSelectedItem().toString());
editor.putString(getKey() + ".value", spinner2.getSelectedItem().toString());
editor.commit();
}
}
}