gpt4 book ai didi

android - 重新创建 Activity 时出现生命周期异常

转载 作者:行者123 更新时间:2023-12-04 12:23:52 26 4
gpt4 key购买 nike

在更改我的应用程序中的语言设置后,我试图重新创建 fragment 和 Activity ,但是 getActivity().recreate();部分正在犯我无法理解的错误。错误是:

E/ActivityInjector: get life cycle exception
当我尝试重新创建 Activity 时发生错误。这是我的 SettingsActivity.java 中的代码:
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);
}
}

最佳答案

我也有同样的问题,我调查的结果是这样的:这是小米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();
}
});
}
}
Activity 主.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)上的按钮 - 没有输出。
而另一款物理设备——小米红米 7(API 29、Android 10、MIUI Global 11.0.1)在每次按下按钮和 Activity 时都会提供此功能:
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)
因此,当我们重新创建 Activity 时,错误:
E/ActivityInjector: get life cycle exception
显然来自 MIUI/小米(至少在这个实验中)。

关于android - 重新创建 Activity 时出现生命周期异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63209993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com