gpt4 book ai didi

android - 为什么 recreate 会无限循环?

转载 作者:行者123 更新时间:2023-11-29 17:24:34 25 4
gpt4 key购买 nike

我有一个简单的程序,可以从 Activity B 更改 Activity A 的背景。当您更改背景时,您需要刷新 Activity A 以使背景发生变化,在查看 stackoverflow 之后,最简单的方法就是调用 recreate()。

我不确定我是否调用错误或在错误的区域但最终发生的是它会在应用程序运行时循环出现以下错误并最终崩溃-

02-01 13:23:53.358 17302-17302/com.package.www.randomapp E/ViewRootImpl: sendUserActionEvent() mView == null

这是 Activity A的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainmenu);
backgroundChanger();
recreate();
}

public void backgroundChanger(){
SharedPreferences sharedGradients = getSharedPreferences("gradientInfo", Context.MODE_PRIVATE);
int backgroundGrad = sharedGradients.getInt("backgroundGradient", 0);


if (backgroundGrad == 0){
MMBackground.setBackgroundResource(R.drawable.blackgreengradiant);




}
if (backgroundGrad == 1){
MMBackground.setBackgroundResource(R.drawable.blueblackgradiant);


}
if (backgroundGrad == 2){
MMBackground.setBackgroundResource(R.drawable.goldblackgradiant);


}

对于 Activity B

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

public void variableHandler() {

MainMenuBackgroundBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {

final SharedPreferences[] sharedGradients = {getSharedPreferences("gradientInfo", Context.MODE_PRIVATE)};
final SharedPreferences.Editor[] editor1 = {sharedGradients[0].edit()};

final SharedPreferences[] sharedBoolean = {getSharedPreferences("binaryPoint", Context.MODE_PRIVATE)};
final SharedPreferences.Editor[] editorBinary = {sharedBoolean[0].edit()};

final PopupMenu popup = new PopupMenu(getApplicationContext(), v);
popup.inflate(R.menu.menu_background_gradiant_setter);
popup.show();


popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {

switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(OptionsMenu.this, "Blue and black gradient", Toast.LENGTH_SHORT).show();

editorBinary[0] = sharedBoolean[0].edit();
editorBinary[0].putInt("binaryPoint", 1);

editor1[0] = sharedGradients[0].edit();
editor1[0].putInt("backgroundGradient", 1);
editor1[0].apply();



return true;

case R.id.item2:
Toast.makeText(OptionsMenu.this, "Gold and black gradient", Toast.LENGTH_SHORT).show();
editorBinary[0] = sharedBoolean[0].edit();
editorBinary[0].putInt("binaryPoint", 1);

editor1[0] = sharedGradients[0].edit();
editor1[0].putInt("backgroundGradient", 2);
editor1[0].apply();
return true;
}
}

最佳答案

问题是您在 Activity 的 onCreate() 方法中调用 recreate() 时没有任何条件,这将创建一个无限循环。保留一个变量来跟踪 Activity 是否重新创建。

private static boolean alreadyRecreated = false;
//You can add some extra conditions here if you want.
if(!alreadyRecreated){
recreate();
alreadyRecreated = true;
}

关于android - 为什么 recreate 会无限循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35137826/

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