gpt4 book ai didi

android - DialogPreference 导致错误 onDisplayPreferenceDialog()

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:59:24 24 4
gpt4 key购买 nike

我想创建一个简单的(是/否)对话框首选项,我遵循了这篇文章中的答案 - 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$1.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 &amp; 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);
}

自定义对话框首选项.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 &amp; 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>

关于android - DialogPreference 导致错误 onDisplayPreferenceDialog(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45512419/

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