android-fragments 重新创建活动时出现生命周期异常

e4yzc0pl  于 2022-11-14  发布在  Android
关注(0)|答案(2)|浏览(224)

我尝试在更改应用中的语言设置后重新创建片段和Activity,但getActivity().recreate();部分出现错误,我无法理解原因。错误是:

E/ActivityInjector: get life cycle exception

当我试图重新创建Activity时,出现了这个错误。

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        getSupportFragmentManager().beginTransaction().replace(R.id.settings, new SettingsFragment()).commit();
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

    }

    public static class SettingsFragment extends PreferenceFragmentCompat {
        ListPreference languages;

        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey);

        }

        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            languages = getPreferenceManager().findPreference("language");
            languages.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    boolean anyChanges=false;
                    if (newValue.toString().equals("turkish")) {
                        LocaleHelper.setLocale(getContext(),"tr-rTR");
                        anyChanges = true;
                    }
                    if (newValue.toString().equals("english")){
                        LocaleHelper.setLocale(getContext(),"en");
                        anyChanges = true;
                    }
                    if (anyChanges){
                        getActivity().recreate();
                    }
                    return true;
                }
            }
            );

            return super.onCreateView(inflater, container, savedInstanceState);
        }
    }
2vuwiymt

2vuwiymt1#

我也有同样的问题,而我对这个问题的调查结果是这样的:这是小米的MIUI中的一个bug,所以我敢打赌你正在使用这个ROM(和我一样),错误发生了。
"我相信我们对此无能为力"
重现步骤
我们只需创建一个新的Android Studio项目,在Activity的布局中添加一个按钮,然后在单击按钮时重新创建()Activity即可。
MainActivity.java:

package com.example;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button b = findViewById(R.id.button);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                recreate();
            }
        });
    }
}

活动_主.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

点击Galaxy Nexus Emulator(API 28,Android 9)上的按钮,在Logcat中会显示以下输出:

2021-03-16 05:47:34.981 5927-6028/com.example D/EGL_emulation: eglMakeCurrent: 0xe3a05120: ver 2 0 (tinfo 0xe3a03290)
2021-03-16 05:47:34.989 5927-6028/com.example D/OpenGLRenderer: endAllActiveAnimators on 0xce2d9080 (RippleDrawable) with handle 0xe3a03600
2021-03-16 05:47:35.077 5927-6028/com.example D/EGL_emulation: eglMakeCurrent: 0xe3a05120: ver 2 0 (tinfo 0xe3a03290)

点击我的第一个真实的的设备上的按钮-三星Galaxy S3(API 25,Android 7.1.2,LineageOS 14.1)-没有输出。
而另一款实体设备-小米Redmi 7(API 29,Android 10,MIUI Global 11.0.1)在每次按键和活动娱乐时都能做到这一点:

2021-03-16 05:54:57.496 10989-10989/com.example W/ActivityThread: SCHED: com.example/.MainActivity [95, r=152ms, a=8ms, w=7395ms]
2021-03-16 05:54:57.517 10989-11032/com.example D/OpenGLRenderer: endAllActiveAnimators on 0x7c2ca87f00 (RippleDrawable) with handle 0x7ccb20d560
2021-03-16 05:54:57.591 10989-10989/com.example E/ActivityInjector: get life cycle exception
    java.lang.ClassCastException: android.os.BinderProxy cannot be cast to android.app.servertransaction.ClientTransaction
        at android.app.ActivityInjector.checkAccessControl(ActivityInjector.java:24)
        at android.app.Activity.onResume(Activity.java:1859)
        at androidx.fragment.app.FragmentActivity.onResume(FragmentActivity.java:456)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1454)
        at android.app.Activity.performResume(Activity.java:8050)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4245)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4287)
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ClientTransactionHandler.executeTransaction(ClientTransactionHandler.java:57)
        at android.app.ActivityThread.handleRelaunchActivityLocally(ActivityThread.java:5320)
        at android.app.ActivityThread.access$3500(ActivityThread.java:223)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2053)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:221)
        at android.app.ActivityThread.main(ActivityThread.java:7542)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

因此,当我们重新创建活动时,错误:

E/ActivityInjector: get life cycle exception

显然来自MIUI/小米(至少在这个实验中)。

pkmbmrz7

pkmbmrz72#

是的,recreate()本身会导致小米设备出现错误(可能是固件或MIUI问题,但他们不在乎)。然而,即使错误显示在日志中,应用程序也不会崩溃,对我来说也是正常的。
我建议将recreate()替换为

Intent i = new Intent(MainActivity.this, MainActivity.class); //change for your case
startActivity(i);

您可以添加overridePendingTransition(0, 0);以避免闪烁。
错误现在消失了。

相关问题