DialogPreference 导致错误 onDisplayPreferenceDialog()
DialogPreference cause error onDisplayPreferenceDialog()
我想创建一个简单的 (yes/no) 对话框首选项,我遵循了此 post - creating a DialogPreference from XML
中的答案
但是我收到一条错误消息
java.lang.IllegalArgumentException: Tried to display dialog for unknown preference type. Did you forget to override onDisplayPreferenceDialog()?
at android.support.v7.preference.PreferenceFragmentCompat.onDisplayPreferenceDialog(PreferenceFragmentCompat.java:647)
at android.support.v7.preference.PreferenceManager.showDialog(PreferenceManager.java:553)
at android.support.v7.preference.DialogPreference.onClick(DialogPreference.java:262)
at android.support.v7.preference.Preference.performClick(Preference.java:1115)
at android.support.v7.preference.Preference.performClick(Preference.java:1100)
at android.support.v7.preference.Preference.onClick(Preference.java:170)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
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:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
我的XML
<android.support.v7.preference.PreferenceCategory android:title="History & privacy">
<com.game.prefs.views.DialogPref
android:key="pref_clear_search"
android:title="Reset Quests"
android:summary="Reset all quest-progress."
android:dialogMessage="Are you sure you wish to reset your quest progress? This action cannot be undone!"
android:positiveButtonText="Clear Quests"
android:negativeButtonText="Cancel"/>
</android.support.v7.preference.PreferenceCategory>
还有我的JAVA代码
public class DialogPref extends DialogPreference {
public DialogPref(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
我做错了什么?
解决方法:在PreferenceFragment.java重写下面的方法
@Override
public void onDisplayPreferenceDialog(Preference preference) {
if (preference instanceof CustomDialogPreference) {
DialogFragment dialogFragment = DialogPrefFragCompat.newInstance(preference.getKey());
dialogFragment.setTargetFragment(this, 0);
dialogFragment.show(getFragmentManager(), null);
} else super.onDisplayPreferenceDialog(preference);
}
CustomDialogPreference .java
public class CustomDialogPreference extends DialogPreference {
public CustomDialogPreference(Context context) {
this(context, null);
}
public CustomDialogPreference(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.dialogPreferenceStyle);
}
public CustomDialogPreference(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, defStyleAttr);
}
public CustomDialogPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
}
DialogPrefFragCompat.java
public class DialogPrefFragCompat extends PreferenceDialogFragmentCompat {
public static DialogPrefFragCompat newInstance(String key) {
final DialogPrefFragCompat fragment = new DialogPrefFragCompat();
final Bundle bundle = new Bundle(1);
bundle.putString(ARG_KEY, key);
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
// do things
}
}
}
xml:
<android.support.v7.preference.PreferenceCategory android:title="History & privacy">
<com.game.prefs.views.CustomDialogPreference
android:key="pref_clear_search"
android:title="Reset Quests"
android:summary="Reset all quest-progress."
android:dialogMessage="Are you sure you wish to reset your quest progress? This action cannot be undone!"
android:dialogTitle=""
android:positiveButtonText="Clear Quests"
android:negativeButtonText="Cancel"/>
</android.support.v7.preference.PreferenceCategory>
我想创建一个简单的 (yes/no) 对话框首选项,我遵循了此 post - creating a DialogPreference from XML
中的答案但是我收到一条错误消息
java.lang.IllegalArgumentException: Tried to display dialog for unknown preference type. Did you forget to override onDisplayPreferenceDialog()?
at android.support.v7.preference.PreferenceFragmentCompat.onDisplayPreferenceDialog(PreferenceFragmentCompat.java:647)
at android.support.v7.preference.PreferenceManager.showDialog(PreferenceManager.java:553)
at android.support.v7.preference.DialogPreference.onClick(DialogPreference.java:262)
at android.support.v7.preference.Preference.performClick(Preference.java:1115)
at android.support.v7.preference.Preference.performClick(Preference.java:1100)
at android.support.v7.preference.Preference.onClick(Preference.java:170)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
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:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
我的XML
<android.support.v7.preference.PreferenceCategory android:title="History & privacy">
<com.game.prefs.views.DialogPref
android:key="pref_clear_search"
android:title="Reset Quests"
android:summary="Reset all quest-progress."
android:dialogMessage="Are you sure you wish to reset your quest progress? This action cannot be undone!"
android:positiveButtonText="Clear Quests"
android:negativeButtonText="Cancel"/>
</android.support.v7.preference.PreferenceCategory>
还有我的JAVA代码
public class DialogPref extends DialogPreference {
public DialogPref(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
我做错了什么?
解决方法:在PreferenceFragment.java重写下面的方法
@Override
public void onDisplayPreferenceDialog(Preference preference) {
if (preference instanceof CustomDialogPreference) {
DialogFragment dialogFragment = DialogPrefFragCompat.newInstance(preference.getKey());
dialogFragment.setTargetFragment(this, 0);
dialogFragment.show(getFragmentManager(), null);
} else super.onDisplayPreferenceDialog(preference);
}
CustomDialogPreference .java
public class CustomDialogPreference extends DialogPreference {
public CustomDialogPreference(Context context) {
this(context, null);
}
public CustomDialogPreference(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.dialogPreferenceStyle);
}
public CustomDialogPreference(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, defStyleAttr);
}
public CustomDialogPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
}
DialogPrefFragCompat.java
public class DialogPrefFragCompat extends PreferenceDialogFragmentCompat {
public static DialogPrefFragCompat newInstance(String key) {
final DialogPrefFragCompat fragment = new DialogPrefFragCompat();
final Bundle bundle = new Bundle(1);
bundle.putString(ARG_KEY, key);
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
// do things
}
}
}
xml:
<android.support.v7.preference.PreferenceCategory android:title="History & privacy">
<com.game.prefs.views.CustomDialogPreference
android:key="pref_clear_search"
android:title="Reset Quests"
android:summary="Reset all quest-progress."
android:dialogMessage="Are you sure you wish to reset your quest progress? This action cannot be undone!"
android:dialogTitle=""
android:positiveButtonText="Clear Quests"
android:negativeButtonText="Cancel"/>
</android.support.v7.preference.PreferenceCategory>