ListPreferences 的异常

发布于 2024-10-05 16:37:48 字数 5744 浏览 4 评论 0原文

这是我的第一个 Android 应用程序,在尝试使用 ListPreference 时遇到了异常。应用程序与首选项一起加载...但是当我触摸 ListPreference 条目时,应用程序“意外停止”。

Settings.java

public class Settings extends PreferenceActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.addPreferencesFromResource(R.layout.settings);
    }
}

settings.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBoxPreference
        android:key="chk_enabled"
        android:summary="SMS response based on settings"
        android:title="Enable"
    />
    <ListPreference
        android:title="Contacts"
        android:summary="Contacs that will be sent SMSs"
        android:key="list_contacts"
        android:defaultValue="0"
        android:entries="@array/list_entries"
        android:entryValues="@array/list_values" 
    />
</PreferenceScreen>

arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="list_entries">
        <item>All</item>
        <item>WhiteList</item>
        <item>BlackList</item>
    </string-array>

    <integer-array name="list_values">
        <item>0</item>
        <item>1</item>
        <item>2</item>
    </integer-array>
</resources>

这是 logcat 输出:

D/AndroidRuntime( 3187): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
D/AndroidRuntime( 3187): CheckJNI is OFF
D/AndroidRuntime( 3187): --- registering native functions ---
I/jdwp    ( 3187): received file descriptor 16 from ADB
/ddm-heap( 3187): Got feature list request
I/ActivityManager(   86): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=tml.UI.main/.MainActivity }
D/AndroidRuntime( 3187): Shutting down VM
D/dalvikvm( 3187): DestroyJavaVM waiting for non-daemon threads to exit
I/ActivityManager(   86): Start proc tml.UI.main for activity tml.UI.main/.MainActivity: pid=3194 uid=10039 gids={1015}
D/dalvikvm( 3187): DestroyJavaVM shutting VM down
D/dalvikvm( 3187): HeapWorker thread shutting down
D/dalvikvm( 3187): HeapWorker thread has shut down
D/jdwp    ( 3187): JDWP shutting down net...
D/jdwp    ( 3187): +++ peer disconnected
I/dalvikvm( 3187): Debugger has detached; object registry had 1 entries
D/dalvikvm( 3187): VM cleaning up
D/dalvikvm( 3187): LinearAlloc 0x0 used 676380 of 4194304 (16%)
I/jdwp    ( 3194): received file descriptor 10 from ADB
D/ddm-heap( 3194): Got feature list request
W/Resources( 3194): Converting to string: TypedValue{t=0x10/d=0x0 a=-1}
I/ActivityManager(   86): Displayed activity tml.UI.main/.MainActivity: 977 ms (total 977 ms)
D/AndroidRuntime( 3194): Shutting down VM
W/dalvikvm( 3194): threadid=3: thread exiting with uncaught exception (group=0x4001da28)
E/AndroidRuntime( 3194): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 3194): java.lang.NullPointerException
E/AndroidRuntime( 3194):    at android.preference.ListPreference.findIndexOfValue(ListPreference.java:169)
E/AndroidRuntime( 3194):    at android.preference.ListPreference.getValueIndex(ListPreference.java:178)
E/AndroidRuntime( 3194):    at android.preference.ListPreference.onPrepareDialogBuilder(ListPreference.java:190)
E/AndroidRuntime( 3194):    at android.preference.DialogPreference.showDialog(DialogPreference.java:291)
E/AndroidRuntime( 3194):    at android.preference.DialogPreference.onClick(DialogPreference.java:262)
E/AndroidRuntime( 3194):    at android.preference.Preference.performClick(Preference.java:811)
E/AndroidRuntime( 3194):    at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:190)
E/AndroidRuntime( 3194):    at android.widget.AdapterView.performItemClick(AdapterView.java:284)
E/AndroidRuntime( 3194):    at android.widget.ListView.performItemClick(ListView.java:3246)
E/AndroidRuntime( 3194):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1635)
E/AndroidRuntime( 3194):    at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime( 3194):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 3194):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 3194):    at android.app.ActivityThread.main(ActivityThread.java:4203)
E/AndroidRuntime( 3194):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 3194):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 3194):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
E/AndroidRuntime( 3194):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
E/AndroidRuntime( 3194):    at dalvik.system.NativeStart.main(Native Method)
I/Process (   86): Sending signal. PID: 3194 SIG: 3
I/dalvikvm( 3194): threadid=7: reacting to signal 3
I/dalvikvm( 3194): Wrote stack trace to '/data/anr/traces.txt'
I/Process ( 3194): Sending signal. PID: 3194 SIG: 9
I/ActivityManager(   86): Process tml.UI.main (pid 3194) has died.
I/WindowManager(   86): WIN DEATH: Window{4341fd00 tml.UI.main/tml.UI.main.MainActivity paused=false}
W/UsageStats(   86): Unexpected resume of com.android.launcher while already resumed in tml.UI.main
W/InputManagerService(   86): Got RemoteException sending setActive(false) notification to pid 3194 uid 10039

This is my first Android app and I've encountered an exception when trying to use the ListPreference. The application loads along with the preferences... but when i touch the ListPreference entry the applications "stops unexpectedly".

Settings.java

public class Settings extends PreferenceActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.addPreferencesFromResource(R.layout.settings);
    }
}

settings.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBoxPreference
        android:key="chk_enabled"
        android:summary="SMS response based on settings"
        android:title="Enable"
    />
    <ListPreference
        android:title="Contacts"
        android:summary="Contacs that will be sent SMSs"
        android:key="list_contacts"
        android:defaultValue="0"
        android:entries="@array/list_entries"
        android:entryValues="@array/list_values" 
    />
</PreferenceScreen>

arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="list_entries">
        <item>All</item>
        <item>WhiteList</item>
        <item>BlackList</item>
    </string-array>

    <integer-array name="list_values">
        <item>0</item>
        <item>1</item>
        <item>2</item>
    </integer-array>
</resources>

This is the logcat output:

D/AndroidRuntime( 3187): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
D/AndroidRuntime( 3187): CheckJNI is OFF
D/AndroidRuntime( 3187): --- registering native functions ---
I/jdwp    ( 3187): received file descriptor 16 from ADB
/ddm-heap( 3187): Got feature list request
I/ActivityManager(   86): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=tml.UI.main/.MainActivity }
D/AndroidRuntime( 3187): Shutting down VM
D/dalvikvm( 3187): DestroyJavaVM waiting for non-daemon threads to exit
I/ActivityManager(   86): Start proc tml.UI.main for activity tml.UI.main/.MainActivity: pid=3194 uid=10039 gids={1015}
D/dalvikvm( 3187): DestroyJavaVM shutting VM down
D/dalvikvm( 3187): HeapWorker thread shutting down
D/dalvikvm( 3187): HeapWorker thread has shut down
D/jdwp    ( 3187): JDWP shutting down net...
D/jdwp    ( 3187): +++ peer disconnected
I/dalvikvm( 3187): Debugger has detached; object registry had 1 entries
D/dalvikvm( 3187): VM cleaning up
D/dalvikvm( 3187): LinearAlloc 0x0 used 676380 of 4194304 (16%)
I/jdwp    ( 3194): received file descriptor 10 from ADB
D/ddm-heap( 3194): Got feature list request
W/Resources( 3194): Converting to string: TypedValue{t=0x10/d=0x0 a=-1}
I/ActivityManager(   86): Displayed activity tml.UI.main/.MainActivity: 977 ms (total 977 ms)
D/AndroidRuntime( 3194): Shutting down VM
W/dalvikvm( 3194): threadid=3: thread exiting with uncaught exception (group=0x4001da28)
E/AndroidRuntime( 3194): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 3194): java.lang.NullPointerException
E/AndroidRuntime( 3194):    at android.preference.ListPreference.findIndexOfValue(ListPreference.java:169)
E/AndroidRuntime( 3194):    at android.preference.ListPreference.getValueIndex(ListPreference.java:178)
E/AndroidRuntime( 3194):    at android.preference.ListPreference.onPrepareDialogBuilder(ListPreference.java:190)
E/AndroidRuntime( 3194):    at android.preference.DialogPreference.showDialog(DialogPreference.java:291)
E/AndroidRuntime( 3194):    at android.preference.DialogPreference.onClick(DialogPreference.java:262)
E/AndroidRuntime( 3194):    at android.preference.Preference.performClick(Preference.java:811)
E/AndroidRuntime( 3194):    at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:190)
E/AndroidRuntime( 3194):    at android.widget.AdapterView.performItemClick(AdapterView.java:284)
E/AndroidRuntime( 3194):    at android.widget.ListView.performItemClick(ListView.java:3246)
E/AndroidRuntime( 3194):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1635)
E/AndroidRuntime( 3194):    at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime( 3194):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 3194):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 3194):    at android.app.ActivityThread.main(ActivityThread.java:4203)
E/AndroidRuntime( 3194):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 3194):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 3194):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
E/AndroidRuntime( 3194):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
E/AndroidRuntime( 3194):    at dalvik.system.NativeStart.main(Native Method)
I/Process (   86): Sending signal. PID: 3194 SIG: 3
I/dalvikvm( 3194): threadid=7: reacting to signal 3
I/dalvikvm( 3194): Wrote stack trace to '/data/anr/traces.txt'
I/Process ( 3194): Sending signal. PID: 3194 SIG: 9
I/ActivityManager(   86): Process tml.UI.main (pid 3194) has died.
I/WindowManager(   86): WIN DEATH: Window{4341fd00 tml.UI.main/tml.UI.main.MainActivity paused=false}
W/UsageStats(   86): Unexpected resume of com.android.launcher while already resumed in tml.UI.main
W/InputManagerService(   86): Got RemoteException sending setActive(false) notification to pid 3194 uid 10039

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

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

发布评论

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

评论(2

看海 2024-10-12 16:37:48

我修复了它...我不断尝试,最后发现我无法使用整数数组作为 entryValues 源。我刚刚将该整数数组更改为字符串数组并使其正常工作。

如果有办法使用整数数组作为 entryValues 的源,请发表评论。

I fixed it... I kept on trying and, at the end, it turns out that I cannot use an integer-array as the entryValues Source. I just changed that integer-array to a string-array and got it working.

If there's a way to use integer-array as the source for entryValues please comment.

鹿港小镇 2024-10-12 16:37:48

我一直在寻找与您正在寻找的类似的解决方案,同意您应该能够使用整数数组来读取和存储整数结果。

我发现这个问题的解决方案是 http://kvance.livejournal.com/1039349.html,我发现这很有用。

它并没有完全按照我的想法或我认为你想要的方式解决问题,相反,你仍然使用字符串数组指定初始/选择值,尽管当 ListPreference 将其保存回 SharedPreferences 时,它会保存为整数而不是字符串(在用户选择它时进行转换),从而无需在访问首选项值时在整个应用程序中多次转换它。

I've been searching for a similar solution to what you are looking for, agreeing that you should be able to use an Integer array to read from and store an integer result.

A solution to this problem I found was at http://kvance.livejournal.com/1039349.html, which I found to be useful.

It doesn't quite solve the problem the way I was thinking or that way I think you want it to, instead you still specify your initial / selection values with a String array although when the ListPreference saves it back to the SharedPreferences it saves as an Integer instead of a String (doing the conversion when the user selects it), removing the need to have to convert it possibly many times throughout your application when accessing the preference value.

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