当我使用targetSdkVersion 26时,我的代码运行良好,现在我已将targetSdkVersion更改为28,所以现在在某些8.0+版本的android设备上出现这些崩溃,该怎么办...?

我已将UrduSimplePreferenceFragment从受保护更改为公共,但仍然遇到相同的崩溃

e8.java

public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager()
                .beginTransaction()
                .replace(android.R.id.content,
                        new UrduSimplePreferenceFragment()).commit();

        mSharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        editor = mSharedPreferences.edit();
    }
    @SuppressLint("validFragment")
    public class  UrduSimplePreferenceFragment extends PreferenceFragment {
        @SuppressLint("NewApi")
        @Override
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.z4);
            Log.e("kk", "onCreate");
            InterstitialAdmob();


            getActionBar().setIcon(R.drawable.icon);
            Preference pref = findPreference("changetheme");
            pref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                @Override
                public boolean onPreferenceClick(Preference preference) {
                    themesDialog();
                    return true;
                }
            });
            /*if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
            }*/

        }

    }


Logcat

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.multi.keyboard.khmer.keyboard.khmerlanguage/com.multi.keyboard.khmer.keyboard.khmerlanguage.e8}: java.lang.IllegalStateException: Fragment com.multi.keyboard.khmer.keyboard.khmerlanguage.e8.UrduSimplePreferenceFragment must be a public static class to be  properly recreated from instance state.
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6942)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
 Caused by: java.lang.IllegalStateException: Fragment com.multi.keyboard.khmer.keyboard.khmerlanguage.e8.UrduSimplePreferenceFragment must be a public static class to be  properly recreated from instance state.
    at android.app.BackStackRecord.doAddOp(BackStackRecord.java:429)
    at android.app.BackStackRecord.replace(BackStackRecord.java:470)
    at android.app.BackStackRecord.replace(BackStackRecord.java:462)
    at com.multi.keyboard.khmer.keyboard.khmerlanguage.e8.onCreate(e8.java:36)
    at android.app.Activity.performCreate(Activity.java:7183)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1221)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6942) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

最佳答案

UrduSimplePreferenceFragment must be a public static class to be  properly recreated from instance state.


将UrduSimplePreferenceFragment设为公共静态类

public static class UrduSimplePreferenceFragment extends PreferenceFragment {
    @SuppressLint("NewApi")
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.z4);
        Log.e("kk", "onCreate");
        InterstitialAdmob();


        getActionBar().setIcon(R.drawable.icon);
        Preference pref = findPreference("changetheme");
        pref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {
                themesDialog();
                return true;
            }
        });
        /*if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }*/

    }

}

关于java - java.lang.IllegalStateException: fragment ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55946480/

10-09 01:20