简体   繁体   中英

Unable to instantiate fragment android.support.v14.preference.PreferenceFragment

I'm trying to use PreferenceFragment from the v7 support library for settings of my app. When I try to run it in android 4.2.2, it always returns the following exception:

E/AndroidRuntime: FATAL EXCEPTION: main
                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{fr.mobiho.navapp/fr.mobiho.navapp.settings.activity.PrefsActivity}: android.app.Fragment$InstantiationException: Unable to instantiate fragment android.support.v14.preference.PreferenceFragment: make sure class name exists, is public, and has an empty constructor that is public
                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
                                                                  at android.app.ActivityThread.access$600(ActivityThread.java:141)
                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                  at android.os.Looper.loop(Looper.java:137)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5041)
                                                                  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:793)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                                                                  at dalvik.system.NativeStart.main(Native Method)
                                                               Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment android.support.v14.preference.PreferenceFragment: make sure class name exists, is public, and has an empty constructor that is public
                                                                  at android.app.Fragment.instantiate(Fragment.java:596)
                                                                  at android.preference.PreferenceActivity.switchToHeaderInner(PreferenceActivity.java:1138)
                                                                  at android.preference.PreferenceActivity.switchToHeader(PreferenceActivity.java:1154)
                                                                  at android.preference.PreferenceActivity.onCreate(PreferenceActivity.java:560)
                                                                  at fr.mobiho.navapp.activity.AppCompatPreferenceActivity.onCreate(AppCompatPreferenceActivity.java:35)
                                                                  at fr.mobiho.navapp.settings.activity.ThemableSettingsActivity.onCreate(ThemableSettingsActivity.java:39)
                                                                  at fr.mobiho.navapp.settings.activity.PrefsActivity.onCreate(PrefsActivity.java:53)
                                                                  at android.app.Activity.performCreate(Activity.java:5104)
                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
                                                                  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
                                                                  at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                  at android.os.Looper.loop(Looper.java:137) 
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5041) 
                                                                  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:793) 
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
                                                                  at dalvik.system.NativeStart.main(Native Method) 
                                                               Caused by: java.lang.InstantiationException: can't instantiate class android.support.v14.preference.PreferenceFragment
                                                                  at java.lang.Class.newInstanceImpl(Native Method)
                                                                  at java.lang.Class.newInstance(Class.java:1319)
                                                                  at android.app.Fragment.instantiate(Fragment.java:585)
                                                                  at android.preference.PreferenceActivity.switchToHeaderInner(PreferenceActivity.java:1138) 
                                                                  at android.preference.PreferenceActivity.switchToHeader(PreferenceActivity.java:1154) 
                                                                  at android.preference.PreferenceActivity.onCreate(PreferenceActivity.java:560) 
                                                                  at fr.mobiho.navapp.activity.AppCompatPreferenceActivity.onCreate(AppCompatPreferenceActivity.java:35) 
                                                                  at fr.mobiho.navapp.settings.activity.ThemableSettingsActivity.onCreate(ThemableSettingsActivity.java:39) 
                                                                  at fr.mobiho.navapp.settings.activity.PrefsActivity.onCreate(PrefsActivity.java:53) 
                                                                  at android.app.Activity.performCreate(Activity.java:5104) 
                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
                                                                  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
                                                                  at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                  at android.os.Looper.loop(Looper.java:137) 
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5041) 
                                                                  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:793) 
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
                                                                  at dalvik.system.NativeStart.main(Native Method) 

But in higher version it's work fine like 5.0, 5.1, 6.0.

Here is the class AppCompatPreferenceActivity

public abstract class AppCompatPreferenceActivity extends PreferenceActivity {

    private AppCompatDelegate mDelegate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        overridePendingTransition(R.anim.slide_in_from_right, R.anim.fade_out_scale);
        getDelegate().installViewFactory();
        getDelegate().onCreate(savedInstanceState);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        getDelegate().onPostCreate(savedInstanceState);
    }

    protected final ActionBar getSupportActionBar() {
        return getDelegate().getSupportActionBar();
    }

    protected final void setSupportActionBar(@Nullable Toolbar toolbar) {
        getDelegate().setSupportActionBar(toolbar);
    }

    @NonNull
    @Override
    public MenuInflater getMenuInflater() {
        return getDelegate().getMenuInflater();
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        getDelegate().setContentView(layoutResID);
    }

    @Override
    public void setContentView(View view) {
        getDelegate().setContentView(view);
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        getDelegate().setContentView(view, params);
    }

    @Override
    public void addContentView(View view, ViewGroup.LayoutParams params) {
        getDelegate().addContentView(view, params);
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
        getDelegate().onPostResume();
    }

    @Override
    protected void onTitleChanged(CharSequence title, int color) {
        super.onTitleChanged(title, color);
        getDelegate().setTitle(title);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        getDelegate().onConfigurationChanged(newConfig);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (isFinishing()) {
            overridePendingTransition(R.anim.fade_in_scale, R.anim.slide_out_to_right);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        getDelegate().onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getDelegate().onDestroy();
    }

    public void invalidateOptionsMenu() {
        getDelegate().invalidateOptionsMenu();
    }

    private AppCompatDelegate getDelegate() {
        if (mDelegate == null) {
            mDelegate = AppCompatDelegate.create(this, null);
        }
        return mDelegate;
    }
}

And here is the MyCustomPreferenceFragment

public class MyCustomPreferenceFragment extends PreferenceFragment {

    @Inject
    PreferenceManager mPreferenceManager;

    public MyCustomPreferenceFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

    }
}

You have to remove abstract keyword because abstract classes cannot be instantiated,so use

public /*abstract*/ class AppCompatPreferenceActivity extends PreferenceActivity { 

For further reading Abstract Methods and Classes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM