从创建XML对话框preference [英] creating a DialogPreference from XML

查看:91
本文介绍了从创建XML对话框preference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用机器人。preference.Dialog preference 从XML膨胀,但文档似乎缺少一些重要的位,我无法找到一个工作示例的任何地方。我的XML现在看起来是这样(我试过很多排列,但是这似乎是一个合理的最低):

 <对话preference
机器人:关键=funThing机器人:标题=有趣的事情
机器人:dialogLayout =@布局/ fun_layout
机器人:positiveButtonText =OK
机器人:negativeButtonText =取消
/>
 

我在这一点假设是,它需要继承对话preference ,而不是直接使用它。首先,我不能找到一种方法,实际的preference值对话框本身中的元素,这在反射是怎样的一个赠品的关联。而且,在看<一href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/$p$pference/Dialog$p$pference.java">the源对话preference.java 似乎也印证了它。而且我也只注意到官方文档是指它作为基地班。但最起码,这将是很好建立网上一个明确的,足够的来源,这将有助于在未来N多人算出这个比我快。

有关的记录,日志文件看起来是这样的:

  I / ActivityManager(61):从PID 2755意向书{CMP = org.jeremy.android /.preferencesActivity}:启动
W /资源(2755):转换为字符串:{的TypedValue T = 0×10 / D = 0x4b0一个= -1}
W /资源(2755):转换为字符串:{的TypedValue T = 0×10 / D = 0x20的一个= -1}
D / AndroidRuntime(2755):关闭虚拟机
W / dalvikvm(2755):主题ID = 1:螺纹退出与未捕获的异常(组= 0x40015560)
E / AndroidRuntime(2755):致命异常:主要
E / AndroidRuntime(2755):java.lang.RuntimeException的:无法启动的活动ComponentInfo {org.jeremy.android/org.jeremy.android.$p$pferencesActivity}:android.view.InflateException:二进制XML文件中的行#28:错误充气类java.lang.reflect.Constructor中
E / AndroidRuntime(2755):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
E / AndroidRuntime(2755):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E / AndroidRuntime(2755):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
E / AndroidRuntime(2755):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
E / AndroidRuntime(2755):在android.os.Handler.dispatchMessage(Handler.java:99)
E / AndroidRuntime(2755):在android.os.Looper.loop(Looper.java:123)
E / AndroidRuntime(2755):在android.app.ActivityThread.main(ActivityThread.java:3683)
E / AndroidRuntime(2755):在java.lang.reflect.Method.invokeNative(本机方法)
E / AndroidRuntime(2755):在java.lang.reflect.Method.invoke(Method.java:507)
E / AndroidRuntime(2755):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
E / AndroidRuntime(2755):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E / AndroidRuntime(2755):在dalvik.system.NativeStart.main(本机方法)
E / AndroidRuntime(2755):android.view.InflateException:二进制XML文件中的行#28:错误充气类java.lang.reflect.Constructor中的产生的原因
E / AndroidRuntime(2755):在安卓preference.GenericInflater.createItem(GenericInflater.java:397)。
E / AndroidRuntime(2755):在安卓preference.GenericInflater.onCreateItem(GenericInflater.java:417)。
E / AndroidRuntime(2755):在安卓preference.GenericInflater.createItemFromTag(GenericInflater.java:428)。
E / AndroidRuntime(2755):在安卓preference.GenericInflater.rInflate(GenericInflater.java:481)。
E / AndroidRuntime(2755):在安卓preference.GenericInflater.rInflate(GenericInflater.java:493)。
E / AndroidRuntime(2755):在安卓preference.GenericInflater.inflate(GenericInflater.java:326)。
E / AndroidRuntime(2755):在安卓preference.GenericInflater.inflate(GenericInflater.java:263)。
E / AndroidRuntime(2755):在安卓preference preferenceManager.inflateFromResource(preferenceManager.java:251)。
E / AndroidRuntime(2755):在安卓preference preferenceActivity.add preferencesFromResource(preferenceActivity.java:262)
E / AndroidRuntime(2755):在org.jeremy.android preferencesActivity.onCreate(preferencesActivity.java:40)。
E / AndroidRuntime(2755):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E / AndroidRuntime(2755):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
E / AndroidRuntime(2755):11 ...更多
E / AndroidRuntime(2755):java.lang.InstantiationException:所致。安卓preference.Dialog preference
E / AndroidRuntime(2755):在java.lang.reflect.Constructor.constructNative(本机方法)
E / AndroidRuntime(2755):在java.lang.reflect.Constructor.newInstance(Constructor.java:415)
E / AndroidRuntime(2755):在安卓preference.GenericInflater.createItem(GenericInflater.java:383)。
E / AndroidRuntime(2755):22 ...更多
W / ActivityManager(61):强制完成活动org.jeremy.android/.$p$pferencesActivity
W / ActivityManager(61):强制完成活动org.jeremy.android/.SplashActivity
 

解决方案

这一个是奇怪的,你继承对话preference 。子类不需要做任何事情。因此,

 公共类MyDialog preference扩展对话框preference {

    公共MyDialog preference(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        // TODO自动生成构造函数存根
    }

}
 

可以被实例化。虽然飞机的老对话preference 不能。很奇怪,他们应该是完全一样的东西。

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

解决方案

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.

这篇关于从创建XML对话框preference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆