当我的应用程序在平板电脑上运行时出现以下错误,尽管它似乎在 nexus 7 的模拟器中运行良好:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.myapp/com.my.myapp.PrefsActivity}: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.my.myapp.PrefsActivity$GeneralPreferenceFragment: make sure class name exists, is public, and has an empty constructor that is public
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2063)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2088)
at android.app.ActivityThread.access$600(ActivityThread.java:126)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1168)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4561)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.my.myapp.PrefsActivity$GeneralPreferenceFragment: make sure class name exists, is public, and has an empty constructor that is public
at android.app.Fragment.instantiate(Fragment.java:581)
at android.preference.PreferenceActivity.switchToHeaderInner(PreferenceActivity.java:1117)
at android.preference.PreferenceActivity.switchToHeader(PreferenceActivity.java:1150)
at android.preference.PreferenceActivity.onCreate(PreferenceActivity.java:551)
at com.my.myapp.PrefsActivity.onCreate(SourceFile:40)
at android.app.Activity.performCreate(Activity.java:4637)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2027)
... 11 more
Caused by: java.lang.ClassNotFoundException: com.my.myapp.PrefsActivity$GeneralPreferenceFragment
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.app.Fragment.instantiate(Fragment.java:571)
... 18 more
我正在使用统一偏好库,我将 GeneralPreferenceFragment 放在它的输出类(而不是内部类)中,但这并没有解决问题
package com.my.myapp;
import net.saik0.android.unifiedpreference.UnifiedPreferenceFragment;
public class GeneralPreferenceFragment extends UnifiedPreferenceFragment {
public GeneralPreferenceFragment(){
}
}
GeneralPreferenceFragment 仅在 prefs_headers.xml 中的 XML 中提及:
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:unified="http://schemas.android.com/apk/res-auto" >
<!--
/*
** Header definitions are identical to android:* except for preferenceRes which should be a reference to a preference xml file
** Unlike the native headers these are also used for building the single pane version.
*/
-->
<header
unified:fragment="com.my.myapp.PrefsActivity$GeneralPreferenceFragment"
unified:title="@string/pref_header_general"
unified:preferenceRes="@xml/pref_general" />
AndroidManifest.xml
<activity
android:name="com.my.myapp.PrefsActivity"
android:configChanges="keyboardHidden|orientation"
>
</activity>