开发者

NullPointerException on createViewFromResource in a PreferenceActivity

开发者 https://www.devze.com 2023-03-31 01:43 出处:网络
I am working on a PreferenceActivity for Android. I am following some of the code discussed here, which uses a ListPreference object. My preferences.xml is as follows:

I am working on a PreferenceActivity for Android. I am following some of the code discussed here, which uses a ListPreference object. My preferences.xml is as follows:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory android:title="@string/prefs_app" >

      <ListPreference
        android:key="@string/gps_freq_main_key"
        android:title="@string/gps_freq_main_title"
        android:summary="@string/gps_freq_main_summary"
        android:entries="@array/gps_freq_main_entries"
        android:entry开发者_JAVA技巧Values="@array/gps_freq_main_entryValues"
        android:defaultValue="every second (recommended)" />
    </PreferenceCategory>

    <PreferenceCategory
        android:title="@string/prefs_other">

        <PreferenceScreen
            android:key="@string/app_info_key"
            android:title="@string/app_info_title"
            android:summary="@string/app_info_summary">

          <Preference
            android:key="@string/app_copyright_key"
            android:title="@string/app_copyright_title"
            android:summary="@string/app_copyright_summary" />

          <Preference
            android:key="@string/app_version_key"
            android:title="@string/app_version_title" />

    </PreferenceScreen>
</PreferenceCategory>

All of the strings mentioned are defined in res/values/strings.xml. The two arrays are defined in res/values/arrays.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <integer-array name="gps_freq_main_entries">
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
    <item>5</item>
    <item>10</item>
    <item>20</item>
    <item>30</item>
    <item>60</item>
    <item>120</item>
    <item>300</item>
    <item>600</item>
    <item>900</item>
    <item>1200</item>
  </integer-array>
  <string-array name="gps_freq_main_entryValues">
    <item>every second (recommended)</item>
    <item>2 seconds</item>
    <item>3 seconds</item>
    <item>4 seconds</item>
    <item>5 seconds</item>
    <item>10 seconds</item>
    <item>20 seconds</item>
    <item>30 seconds</item>
    <item>1 minute</item>
    <item>2 minutes</item>
    <item>5 minutes</item>
    <item>10 minutes</item>
    <item>15 minutes</item>
    <item>30 minutes</item>
  </string-array>

</resources>

The problem arises when I select this ListPreference (the Activity launches correctly, and all other preferences work). There is no code in my PreferenceActivity that affects this ListPreference. Here is my logcat:

08-24 15:34:49.428: ERROR/AndroidRuntime(1250): Uncaught handler: thread main exiting due to uncaught exception
08-24 15:34:49.478: ERROR/AndroidRuntime(1250): java.lang.NullPointerException
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:351)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.widget.AbsListView.obtainView(AbsListView.java:1269)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1117)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.widget.ListView.onMeasure(ListView.java:1030)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.view.View.measure(View.java:7115)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2875)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.view.View.measure(View.java:7115)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2875)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.view.View.measure(View.java:7115)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2875)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.view.View.measure(View.java:7115)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2875)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.view.View.measure(View.java:7115)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.view.ViewRoot.performTraversals(ViewRoot.java:698)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1482)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.os.Looper.loop(Looper.java:123)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at android.app.ActivityThread.main(ActivityThread.java:3948)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at java.lang.reflect.Method.invokeNative(Native Method)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at java.lang.reflect.Method.invoke(Method.java:521)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
08-24 15:34:49.478: ERROR/AndroidRuntime(1250):     at dalvik.system.NativeStart.main(Native Method)

Any help would be greatly appreciated!


I just ran into this problem with a different application. To solve it, I used a string-array instead of an integer array. I'm not sure why this is needed, but it worked.

0

精彩评论

暂无评论...
验证码 换一张
取 消