从 XML 创建 DialogPreference

发布于 2024-10-24 09:28:48 字数 4767 浏览 4 评论 0 原文

我一直在尝试使用从 XML 扩展的 android.preference.DialogPreference ,但文档似乎缺少一些重要的部分,而且我在任何地方都找不到工作示例。我的 XML 现在看起来像这样(我尝试了很多排列,但这似乎是一个合理的最小值):

<DialogPreference
android:key="funThing" android:title="Fun Thing"
android:dialogLayout="@layout/fun_layout"
android:positiveButtonText="OK"
android:negativeButtonText="Cancel"
/>

我此时的假设是需要子类 DialogPreference 而不是直接使用它。首先,我找不到一种方法将实际偏好值与对话框本身中的元素相关联,这在反思中是一种赠品。另外,查看 DialogPreference.java 的源代码似乎证实了这一点。我还注意到 官方文档 将其称为“基类”。但至少,最好在网上建立一个足够明确的来源,这将有助于接下来的 N 个人比我更快地弄清楚这一点。

作为记录,日志文件如下所示:

I/ActivityManager(   61): Starting: Intent { cmp=org.jeremy.android/.PreferencesActivity } from pid 2755
W/Resources( 2755): Converting to string: TypedValue{t=0x10/d=0x4b0 a=-1}
W/Resources( 2755): Converting to string: TypedValue{t=0x10/d=0x20 a=-1}
D/AndroidRuntime( 2755): Shutting down VM
W/dalvikvm( 2755): threadid=1: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime( 2755): FATAL EXCEPTION: main
E/AndroidRuntime( 2755): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.jeremy.android/org.jeremy.android.PreferencesActivity}: android.view.InflateException: Binary XML file line #28: Error inflating class java.lang.reflect.Constructor
E/AndroidRuntime( 2755):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
E/AndroidRuntime( 2755):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRuntime( 2755):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime( 2755):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime( 2755):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 2755):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 2755):    at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime( 2755):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2755):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 2755):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 2755):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 2755):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 2755): Caused by: android.view.InflateException: Binary XML file line #28: Error inflating class java.lang.reflect.Constructor
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.createItem(GenericInflater.java:397)
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.inflate(GenericInflater.java:326)
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.inflate(GenericInflater.java:263)
E/AndroidRuntime( 2755):    at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:251)
E/AndroidRuntime( 2755):    at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:262)
E/AndroidRuntime( 2755):    at org.jeremy.android.PreferencesActivity.onCreate(PreferencesActivity.java:40)
E/AndroidRuntime( 2755):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 2755):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
E/AndroidRuntime( 2755):    ... 11 more
E/AndroidRuntime( 2755): Caused by: java.lang.InstantiationException: android.preference.DialogPreference
E/AndroidRuntime( 2755):    at java.lang.reflect.Constructor.constructNative(Native Method)
E/AndroidRuntime( 2755):    at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.createItem(GenericInflater.java:383)
E/AndroidRuntime( 2755):    ... 22 more
W/ActivityManager(   61):   Force finishing activity org.jeremy.android/.PreferencesActivity
W/ActivityManager(   61):   Force finishing activity org.jeremy.android/.SplashActivity

I have been attempting to use an android.preference.DialogPreference inflated from XML, but the documentation seems to be missing some essential bits, and I cannot find a working example anywhere. My XML now looks like this (I tried many permutations but this seems like a reasonable minimum):

<DialogPreference
android:key="funThing" android:title="Fun Thing"
android:dialogLayout="@layout/fun_layout"
android:positiveButtonText="OK"
android:negativeButtonText="Cancel"
/>

My supposition at this point is that it is required to subclass DialogPreference and not to use it directly. For one, I cannot find a way to associate the actual preference value with an element in the dialog itself, which upon reflection is kind of a giveaway. And also, looking at the source to DialogPreference.java seems to confirm it. And I also just noticed that the official documentation refers to it as a "base class". But at very least, it would be nice to establish a definitive-enough source on the net which would help the next N people figure this out faster than I did.

For the record, the logfile looks like this:

I/ActivityManager(   61): Starting: Intent { cmp=org.jeremy.android/.PreferencesActivity } from pid 2755
W/Resources( 2755): Converting to string: TypedValue{t=0x10/d=0x4b0 a=-1}
W/Resources( 2755): Converting to string: TypedValue{t=0x10/d=0x20 a=-1}
D/AndroidRuntime( 2755): Shutting down VM
W/dalvikvm( 2755): threadid=1: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime( 2755): FATAL EXCEPTION: main
E/AndroidRuntime( 2755): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.jeremy.android/org.jeremy.android.PreferencesActivity}: android.view.InflateException: Binary XML file line #28: Error inflating class java.lang.reflect.Constructor
E/AndroidRuntime( 2755):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
E/AndroidRuntime( 2755):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRuntime( 2755):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime( 2755):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime( 2755):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 2755):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 2755):    at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime( 2755):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2755):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 2755):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 2755):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 2755):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 2755): Caused by: android.view.InflateException: Binary XML file line #28: Error inflating class java.lang.reflect.Constructor
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.createItem(GenericInflater.java:397)
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.inflate(GenericInflater.java:326)
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.inflate(GenericInflater.java:263)
E/AndroidRuntime( 2755):    at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:251)
E/AndroidRuntime( 2755):    at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:262)
E/AndroidRuntime( 2755):    at org.jeremy.android.PreferencesActivity.onCreate(PreferencesActivity.java:40)
E/AndroidRuntime( 2755):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 2755):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
E/AndroidRuntime( 2755):    ... 11 more
E/AndroidRuntime( 2755): Caused by: java.lang.InstantiationException: android.preference.DialogPreference
E/AndroidRuntime( 2755):    at java.lang.reflect.Constructor.constructNative(Native Method)
E/AndroidRuntime( 2755):    at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.createItem(GenericInflater.java:383)
E/AndroidRuntime( 2755):    ... 22 more
W/ActivityManager(   61):   Force finishing activity org.jeremy.android/.PreferencesActivity
W/ActivityManager(   61):   Force finishing activity org.jeremy.android/.SplashActivity

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

狂之美人 2024-10-31 09:28:48

这是如何使用对话框首选项(如您提到的子类化)的示例。

package dk.myapp.views;

import android.content.Context;
import android.preference.DialogPreference;
import android.util.AttributeSet;

/**
 * The OptionDialogPreference will display a dialog, and will persist the
 * <code>true</code> when pressing the positive button and <code>false</code>
 * otherwise. It will persist to the android:key specified in xml-preference.
 */
public class OptionDialogPreference extends DialogPreference {

    public OptionDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);
        persistBoolean(positiveResult);
    }

}

Preferences.xml 应该包含

<dk.myapp.views.OptionDialogPreference
            android:key="@string/prefKeyResetQuests"
            android:dialogIcon="@android:drawable/ic_dialog_alert"
            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"/>

我有一个 res/value 包含(尽管键名也可以在上面明确声明)。

<string name="prefKeyResetQuests">resetQuests</string>  

我的 PreferenceActivity 从 onPause 进行实际重置。请注意,onStop 可能为时已晚,因为 onStop 并不总是在按回键后立即调用:

@Override
public void onPause() {
    SharedPreferences prefs = android.preference.PreferenceManager.
        getDefaultSharedPreferences(getBaseContext());
    if(prefs.getBoolean(
        getResources().getString(R.string.prefKeyResetQuests), false)) {
        // apply reset, and then set the pref-value back to false
    }
}

或者等效地,因为我们仍在 PreferenceActivity 中:

@Override
protected void onPause() {
    Preference prefResetQuests =
        findPreference(getResources().getString(R.string.prefKeyResetQuests));
    if(prefResetQuests.getSharedPreferences().
        getBoolean(prefResetQuests.getKey(), false)){
        // apply reset, and then set the pref-value back to false 
    }
}

Here is an example of how to use the dialog preference (subclassing as you mentioned).

package dk.myapp.views;

import android.content.Context;
import android.preference.DialogPreference;
import android.util.AttributeSet;

/**
 * The OptionDialogPreference will display a dialog, and will persist the
 * <code>true</code> when pressing the positive button and <code>false</code>
 * otherwise. It will persist to the android:key specified in xml-preference.
 */
public class OptionDialogPreference extends DialogPreference {

    public OptionDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);
        persistBoolean(positiveResult);
    }

}

The preferences.xml should contain

<dk.myapp.views.OptionDialogPreference
            android:key="@string/prefKeyResetQuests"
            android:dialogIcon="@android:drawable/ic_dialog_alert"
            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"/>

I have a res/value containing (though the key-name can also be declared explicitly above).

<string name="prefKeyResetQuests">resetQuests</string>  

My PreferenceActivity does the actual resetting from onPause. Note that onStop may be too late since the onStop will not always be called immediately after pressing back:

@Override
public void onPause() {
    SharedPreferences prefs = android.preference.PreferenceManager.
        getDefaultSharedPreferences(getBaseContext());
    if(prefs.getBoolean(
        getResources().getString(R.string.prefKeyResetQuests), false)) {
        // apply reset, and then set the pref-value back to false
    }
}

Or equivalently since we are still in the PreferenceActivity:

@Override
protected void onPause() {
    Preference prefResetQuests =
        findPreference(getResources().getString(R.string.prefKeyResetQuests));
    if(prefResetQuests.getSharedPreferences().
        getBoolean(prefResetQuests.getKey(), false)){
        // apply reset, and then set the pref-value back to false 
    }
}
北风几吹夏 2024-10-31 09:28:48

这个很奇怪,你需要子类化DialogPreference。子类不需要做任何事情。所以

public class MyDialogPreference extends DialogPreference {

    public MyDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

} 

可以实例化。而旧的 DialogPreference 飞机则不能。很奇怪,它们应该是完全相同的东西。

This one is weird, you need to subclass DialogPreference. The subclass does not need to do anything. So

public class MyDialogPreference extends DialogPreference {

    public MyDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

} 

can be instantiated. While a plane old DialogPreference can not. Very weird, they should be the exact same thing.

夜未央樱花落 2024-10-31 09:28:48

首先:

创建自己的类,它扩展 DialogPreference 如下:

package com.test.view;

import android.preference.DialogPreference;

public class DialogExPreference extends DialogPreference 
{
    public DialogExPreference(Context oContext, AttributeSet attrs)
    {
        super(oContext, attrs);     
    }
}

第二:

修改 xml 文件如下:

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">

    <com.test.view.DialogExPreference
        android:title="@string/title"
        android:dialogMessage="@string/lite"              
                android:negativeButtonText="test"/> 

</PreferenceScreen>

然后就可以了。

First:

Create your own class which extends DialogPreference as bellow:

package com.test.view;

import android.preference.DialogPreference;

public class DialogExPreference extends DialogPreference 
{
    public DialogExPreference(Context oContext, AttributeSet attrs)
    {
        super(oContext, attrs);     
    }
}

Second:

Modify the xml file as bellow:

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">

    <com.test.view.DialogExPreference
        android:title="@string/title"
        android:dialogMessage="@string/lite"              
                android:negativeButtonText="test"/> 

</PreferenceScreen>

Then it's OK.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文