ListPreference 标签导致我的应用程序崩溃
ListPreference tag causes my app to crash
我尝试了来自 Java Code Geek 的示例代码 Android Settings Example。
当我点击设置浮动操作栏时遇到问题:应用程序崩溃是由于:
FATAL EXCEPTION: main
Process: com.sano.tamer.androidsettings, PID: 32035
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.sano.tamer.androidsettings/com.sano.tamer.androidsettings.SettingsActivity}: android.view.InflateException: Binary XML file line #14: Error inflating class java.lang.reflect.Constructor
当我评论 ListPreference 标签时,它起作用了,并打开了首选项 activity,我复制并粘贴了首选项文件,这是什么问题?
我正在使用 Android 工作室 2.1.2
谢谢
我的设备是 Asus Fonepad 7 FE375CG,运行 Kitkat
这里是完整的错误日志:
07-23 07:06:28.629 5034-5034/com.sano.tamer.androidsettings E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sano.tamer.androidsettings, PID: 5034
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sano.tamer.androidsettings/com.sano.tamer.androidsettings.SettingsActivity}: android.view.InflateException: Binary XML file line #14: Error inflating class java.lang.reflect.Constructor
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2318)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access0(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5252)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class java.lang.reflect.Constructor
at android.preference.GenericInflater.createItem(GenericInflater.java:397)
at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
at android.preference.GenericInflater.inflate(GenericInflater.java:326)
at android.preference.GenericInflater.inflate(GenericInflater.java:263)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1472)
at com.sano.tamer.androidsettings.SettingsActivity.onCreate(SettingsActivity.java:11)
at android.app.Activity.performCreate(Activity.java:5403)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access0(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5252)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.preference.GenericInflater.createItem(GenericInflater.java:383)
at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
at android.preference.GenericInflater.inflate(GenericInflater.java:326)
at android.preference.GenericInflater.inflate(GenericInflater.java:263)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1472)
at com.sano.tamer.androidsettings.SettingsActivity.onCreate(SettingsActivity.java:11)
at android.app.Activity.performCreate(Activity.java:5403)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access0(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5252)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.res.AssetManager.getResourceTextArray(AssetManager.java:215)
at android.content.res.Resources.getTextArray(Resources.java:454)
at android.content.res.TypedArray.getTextArray(TypedArray.java:629)
at android.preference.ListPreference.<init>(ListPreference.java:51)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.preference.GenericInflater.createItem(GenericInflater.java:383)
at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
at android.preference.GenericInflater.inflate(GenericInflater.java:326)
at android.preference.GenericInflater.inflate(GenericInflater.java:263)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1472)
at com.sano.tamer.androidsettings.SettingsActivity.onCreate(SettingsActivity.java:11)
at android.app.Activity.performCreate(Activity.java:5403)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access0(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5252)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)
这里实际上发生了错误:
Caused by: java.lang.NullPointerException
at android.content.res.AssetManager.getResourceTextArray(AssetManager.java:215)
at android.content.res.Resources.getTextArray(Resources.java:454)
at android.content.res.TypedArray.getTextArray(TypedArray.java:629)
at android.preference.ListPreference.<init>(ListPreference.java:51)
因此,在读取 TextArray 时似乎是一个空指针。
查看布局 XML,我可以看到您的 ListPreference
使用了两个数组:
<ListPreference
...
android:entries="@array/entries_text"
android:entryValues="@array/entries_values"
... />
因此,请仔细检查您的文件 res/values/string.xml
并确认您已创建两个数组:entries_text
和 entries_values
<string-array name="entries_text">
<item>entries 1</item>
<item>entries 2</item>
<item>entries 3</item>
</string-array>
<string-array name="entries_values">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
我尝试了来自 Java Code Geek 的示例代码 Android Settings Example。
当我点击设置浮动操作栏时遇到问题:应用程序崩溃是由于:
FATAL EXCEPTION: main
Process: com.sano.tamer.androidsettings, PID: 32035
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.sano.tamer.androidsettings/com.sano.tamer.androidsettings.SettingsActivity}: android.view.InflateException: Binary XML file line #14: Error inflating class java.lang.reflect.Constructor
当我评论 ListPreference 标签时,它起作用了,并打开了首选项 activity,我复制并粘贴了首选项文件,这是什么问题? 我正在使用 Android 工作室 2.1.2
谢谢
我的设备是 Asus Fonepad 7 FE375CG,运行 Kitkat
这里是完整的错误日志:
07-23 07:06:28.629 5034-5034/com.sano.tamer.androidsettings E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sano.tamer.androidsettings, PID: 5034
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sano.tamer.androidsettings/com.sano.tamer.androidsettings.SettingsActivity}: android.view.InflateException: Binary XML file line #14: Error inflating class java.lang.reflect.Constructor
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2318)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access0(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5252)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class java.lang.reflect.Constructor
at android.preference.GenericInflater.createItem(GenericInflater.java:397)
at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
at android.preference.GenericInflater.inflate(GenericInflater.java:326)
at android.preference.GenericInflater.inflate(GenericInflater.java:263)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1472)
at com.sano.tamer.androidsettings.SettingsActivity.onCreate(SettingsActivity.java:11)
at android.app.Activity.performCreate(Activity.java:5403)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access0(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5252)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.preference.GenericInflater.createItem(GenericInflater.java:383)
at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
at android.preference.GenericInflater.inflate(GenericInflater.java:326)
at android.preference.GenericInflater.inflate(GenericInflater.java:263)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1472)
at com.sano.tamer.androidsettings.SettingsActivity.onCreate(SettingsActivity.java:11)
at android.app.Activity.performCreate(Activity.java:5403)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access0(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5252)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.res.AssetManager.getResourceTextArray(AssetManager.java:215)
at android.content.res.Resources.getTextArray(Resources.java:454)
at android.content.res.TypedArray.getTextArray(TypedArray.java:629)
at android.preference.ListPreference.<init>(ListPreference.java:51)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.preference.GenericInflater.createItem(GenericInflater.java:383)
at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
at android.preference.GenericInflater.inflate(GenericInflater.java:326)
at android.preference.GenericInflater.inflate(GenericInflater.java:263)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1472)
at com.sano.tamer.androidsettings.SettingsActivity.onCreate(SettingsActivity.java:11)
at android.app.Activity.performCreate(Activity.java:5403)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access0(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5252)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)
这里实际上发生了错误:
Caused by: java.lang.NullPointerException
at android.content.res.AssetManager.getResourceTextArray(AssetManager.java:215)
at android.content.res.Resources.getTextArray(Resources.java:454)
at android.content.res.TypedArray.getTextArray(TypedArray.java:629)
at android.preference.ListPreference.<init>(ListPreference.java:51)
因此,在读取 TextArray 时似乎是一个空指针。
查看布局 XML,我可以看到您的 ListPreference
使用了两个数组:
<ListPreference
...
android:entries="@array/entries_text"
android:entryValues="@array/entries_values"
... />
因此,请仔细检查您的文件 res/values/string.xml
并确认您已创建两个数组:entries_text
和 entries_values
<string-array name="entries_text">
<item>entries 1</item>
<item>entries 2</item>
<item>entries 3</item>
</string-array>
<string-array name="entries_values">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>