简体   繁体   English

在Android中创建偏好活动

[英]Creating a Preference Activity In Android

I'm trying to create a simple preferences activity screen, following the docs . 我正在尝试根据docs创建一个简单的首选项活动屏幕。 I've set it up to launch when an options menu button is pressed, but when I trigger it, my app crashes and I get this useless runtime exception message: 我已经将其设置为在按下选项菜单按钮时启动,但是当我触发它时,我的应用程序崩溃了,并且我收到了这个无用的运行时异常消息:

07-29 21:42:28.879: E/AndroidRuntime(13441): FATAL EXCEPTION: main
07-29 21:42:28.879: E/AndroidRuntime(13441): java.lang.RuntimeException: Unable to start activity ComponentInfo{myapp/myapp.ApplicationPreferenceActivity}: java.lang.ClassCastException: java.lang.Integer
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2737)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.app.ActivityThread.access$2500(ActivityThread.java:129)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2107)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.os.Looper.loop(Looper.java:143)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.app.ActivityThread.main(ActivityThread.java:4701)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at java.lang.reflect.Method.invokeNative(Native Method)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at java.lang.reflect.Method.invoke(Method.java:521)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at dalvik.system.NativeStart.main(Native Method)
07-29 21:42:28.879: E/AndroidRuntime(13441): Caused by: java.lang.ClassCastException: java.lang.Integer
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.app.ContextImpl$SharedPreferencesImpl.getString(ContextImpl.java:2797)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.preference.Preference.getPersistedString(Preference.java:1249)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.preference.ListPreference.onSetInitialValue(ListPreference.java:232)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.preference.Preference.dispatchSetInitialValue(Preference.java:1172)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at andr
oid.preference.Preference.onAttachedToHierarchy(Preference.java:984)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.preference.PreferenceGroup.addPreference(PreferenceGroup.java:156)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:97)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:38)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.preference.GenericInflater.rInflate(GenericInflater.java:488)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.preference.GenericInflater.inflate(GenericInflater.java:326)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.preference.GenericInflater.inflate(GenericInflater.java:263)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:251)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:262)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at myapp.ApplicationPreferenceActivity.onCreate(ApplicationPreferenceActivity.java:71)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2701)
07-29 21:42:28.879: E/AndroidRuntime(13441):    ... 11 more

My preferences configuration file is simply: 我的首选项配置文件很简单:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:title="@string/preferences_title" >
    <ListPreference
        android:defaultValue="1"
        android:entries="@array/units_choices"
        android:entryValues="@array/units_values"
        android:key="units"
        android:summary="@string/units_summary"
        android:title="@string/units_title" />
</PreferenceScreen>

I have the units_choices and units_values defined in res/values/array.xml and res/values/array_values.xml respectively, which are simply: 我分别在res / values / array.xml和res / values / array_values.xml中定义了units_choices和units_values,它们很简单:

<resources
   xmlns:android="http://schemas.android.com/apk/res/android">
   <string-array
      name="units_choices">
      <item>Imperial (miles)</item>
      <item>Metric (meters)</item>
   </string-array>
</resources>

<resources
    xmlns:android="http://schemas.android.com/apk/res/android">
    <string-array name="units_values" translatable="false">
        <item>2</item>
        <item>1</item>
    </string-array>
</resources>

If I comment out the ListPreference tag, the preference screen shows and my app doesn't crash, so I'm assuming the ListPreference is configured incorrectly. 如果我注释掉ListPreference标记,则会显示首选项屏幕,并且我的应用程序不会崩溃,因此我假设ListPreference配置不正确。 What am I doing wrong? 我究竟做错了什么? I'm developing using the Eclipse IDE, and it's showing that all the @ references are satisfied. 我正在使用Eclipse IDE进行开发,它表明所有@引用均已满足。 Why would this single tag crash the entire app? 为什么会这样一个标签崩溃,整个应用程序?

This isn't an answer, but was a bit big for a comment. 这不是答案,但评论有点大。

I assume myapp.ApplicationPreferenceActivity.onCreate(ApplicationPreferenceActivity.java:71) is the line of your ApplicationPreferenceActivity where you are calling setContentView()? 我假设myapp.ApplicationPreferenceActivity.onCreate(ApplicationPreferenceActivity.java:71)是您的ApplicationPreferenceActivity中您要调用setContentView()的行? Thus it is an xml inflate error. 因此,这是一个xml膨胀错误。

A couple of things about your xml file. 有关您的xml文件的几件事。 You don't need to call: 您无需致电:

android:layout_width="match_parent"
android:layout_height="match_parent"

Like in the example from the docs . 就像在docs的示例中一样。

Also, what API level are you using? 另外,您使用的是哪个API级别? Are you aware that from API level 11 much of PreferenceActivity class has been deprecated, and now it uses PreferenceFragments instead? 您是否知道从API级别11开始已经弃用了大部分PreferenceActivity类,现在改为使用PreferenceFragments? So if you want to support Pre Android 3.0 you can use PreferenceActivity, however, if you want to support 3.0 and above you'll have to use PreferenceFragments . 因此,如果要支持Android 3.0之前的版本,可以使用PreferenceActivity,但是,如果要支持3.0及更高版本,则必须使用PreferenceFragments

1. First of all i see a ClassCastException , that will happen when you try to push in an object of wrong type into the Object Reference Variable of UN-Compatible type. 1.首先,我看到ClassCastException ,当您尝试将错误类型的对象推入UN兼容类型的对象引用变量时,将发生这种情况。 As you have not put up the code over here, i am unable to detect it. 由于您尚未在此处放置代码,因此我无法检测到它。

2. As you are now learning about Preference Activity, i will suggest you to see this link 2.在您学习“偏好活动”时,我建议您查看此链接

http://androidresearch.wordpress.com/2012/03/09/creating-a-preference-activity-in-android/ http://androidresearch.wordpress.com/2012/03/09/creating-a-preference-activity-in-android/

The problem was caused by my setting a default unit preference using putInt in my activity's onCreate, whereas the units_values are apparently interpreted as strings even though they're also integers. 问题是由我在活动的onCreate中使用units_values设置默认unit首选项引起的,而units_values显然也是字符串,即使它们也是整数。 When the ListPreference tried to load my default int as a string, that threw the ClassCastException. 当ListPreference尝试将我的默认int作为字符串加载时,抛出了ClassCastException。

I modified my code in onCreate to use putString, cleared my installed app's data, and that fixed the problem. 我在onCreate上修改了代码以使用putString,清除了已安装应用程序的数据,并解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM