我有以下活动:
public class PreferenceActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getFragmentManager().findFragmentById(android.R.id.content)==null) {
getFragmentManager()
.beginTransaction()
.add(android.R.id.content, new Display())
.commit();
}
}
public static class Display extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
}
的preferences.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- A 'parent' preference, which enables/disables child preferences (below)
when checked/unchecked. -->
<SwitchPreference
android:defaultValue="true"
android:key="preferences_app_enabled"
android:summary="Select your favourite"
android:title="@string/app_enabled"/>
<!--
<ListPreference
android:defaultValue="1"
android:key="preferences_translation_type"
android:summary="Select your favourite"
android:title="@string/translation_type"
android:entries="@array/translation_type_items"/>
-->
</PreferenceScreen>
当我取消注释时,XML应用的ListPreference部分在启动时崩溃(PreferenceActivity是一个启动器),但有以下异常:
*PreferenceActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class java.lang.reflect.Constructo*
数组值如下:
<resources>
<string-array name="translation_type_items">
<item>floating_window</item>
<item>notification</item>
<item>toast_message</item>
</string-array>
<string-array name="translation_type_values">
<item>@string/floating_window</item>
<item>@string/notification</item>
<item>@string/toast_message</item>
</string-array>
</resources>
完整堆栈跟踪
10-05 08:31:33.064 31472-31472/domainName.com.bubbletranslate E/AndroidRuntime: FATAL EXCEPTION: main
Process: domainName.com.bubbletranslate, PID: 31472
java.lang.RuntimeException: Unable to start activity ComponentInfo{domainName.com.bubbletranslate/domainName.com.bubbletranslate.activity.PreferenceActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class java.lang.reflect.Constructor
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3738)
at android.app.ActivityThread.access$900(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #12: 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.inflate(GenericInflater.java:326)
at android.preference.GenericInflater.inflate(GenericInflater.java:263)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:285)
at domainName.com.bubbletranslate.activity.PreferenceActivity$Display.onCreate(PreferenceActivity.java:31)
at android.app.Fragment.performCreate(Fragment.java:1678)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:859)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1044)
at android.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1848)
at android.app.Activity.onCreate(Activity.java:902)
at android.support.v4.app.BaseFragmentActivityDonut.onCreate(BaseFragmentActivityDonut.java:41)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:315)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:85)
at domainName.com.bubbletranslate.activity.PreferenceActivity.onCreate(PreferenceActivity.java:17)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3738)
at android.app.ActivityThread.access$900(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
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.inflate(GenericInflater.java:326)
at android.preference.GenericInflater.inflate(GenericInflater.java:263)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:285)
at domainName.com.bubbletranslate.activity.PreferenceActivity$Display.onCreate(PreferenceActivity.java:31)
at android.app.Fragment.performCreate(Fragment.java:1678)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:859)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1044)
at android.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1848)
at android.app.Activity.onCreate(Activity.java:902)
at android.support.v4.app.BaseFragmentActivityDonut.onCreate(BaseFragmentActivityDonut.java:41)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:315)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:85)
at domainName.com.bubbletranslate.activity.PreferenceActivity.onCreate(PreferenceActivity.java:17)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3738)
at android.app.ActivityThread.access$900(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
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:448)
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.inflate(GenericInflater.java:326)
at android.preference.GenericInflater.inflate(GenericInflater.java:263)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:285)
at domainName.com.bubbletranslate.activity.PreferenceActivity$Display.onCreate(PreferenceActivity.java:31)
at android.app.Fragment.performCreate(Fragment.java:1678)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:859)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1044)
at android.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1848)
at android.app.Activity.onCreate(Activity.java:902)
at android.support.v4.app.BaseFragmentActivityDonut.onCreate(BaseFragmentActivityDonut.java:41)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:315)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:85)
at domainName.com.bubbletranslate.activity.PreferenceActivity.onCreate(PreferenceActivity.java:17)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3738)
at android.app.ActivityThread.access$900(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
是什么原因导致这个问题?我怎样才能以正确的方式解决?
非常感谢您的任何建议。
答案 0 :(得分:0)
将an android:entryValues
attribute添加到ListPreference
。
答案 1 :(得分:0)
我通过将字符串数组更改为简单数组值来解决它,如下所示:
<resources>
<string-array name="translation_type_items">
<item>floating_window</item>
<item>notification</item>
<item>toast_message</item>
</string-array>
<array name="translation_type_values">
<item>@string/floating_window</item>
<item>@string/notification</item>
<item>@string/toast_message</item>
</array>
</resources>
对于价值:
android:entries="@array/translation_type_values"