android - 重新创建 Activity 导致无限循环

标签 android

我正在尝试更改我的应用程序的主题,因此当用户按下按钮时我会调用以下方法:

activity.setTheme(R.style.BlueTheme);
activity.recreate();

但是,无论何时发生这种情况,应用程序都会进入无限循环,控制台输出为:

02-01 11:36:15.077 9245-9276/org.example.androidscouting E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab7d5680
02-01 11:36:15.131 9245-9276/org.example.androidscouting W/EGL_emulation: eglSurfaceAttrib not implemented
02-01 11:36:15.131 9245-9276/org.example.androidscouting W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa3170780, error=EGL_SUCCESS

上面的内容一直循环直到我手动关闭应用程序。

谁能告诉我问题是什么以及如何解决它,或者至少为我指出正确的方向?

编辑:附加代码

创建时:

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Utils.onActivityCreateSetTheme(this);
    setContentView(R.layout.activity_scouting);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    LinearLayout l = (LinearLayout) findViewById(R.id.mainLinear);
    l.requestFocus();

    //createTheApp();

    Switch colorSwitch = (Switch) findViewById(R.id.team_color);
    colorSwitch.setOnCheckedChangeListener(this);


}

onCheckedChange():

    @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked){
        Utils.changeToTheme(this,Utils.THEME_RED);

    } else {
        Utils.changeToTheme(this, Utils.THEME_BLUE);
    }
}

Utils.java - 改编自另一个问题

import android.app.Activity;
import android.content.Intent;

public class Utils
{
    private static int sTheme = 2;
    public final static int THEME_RED = 1;
    public final static int THEME_BLUE = 2;

    public static void changeToTheme(Activity activity, int theme)
    {
        sTheme = theme;
        activity.recreate();
    }
    /** Set the theme of the activity, according to the configuration. */
    public static void onActivityCreateSetTheme(Activity activity)
    {
        switch (sTheme)
        {
            default:
            case THEME_BLUE:
                activity.setTheme(R.style.BlueTheme);
                break;
            case THEME_RED:
                activity.setTheme(R.style.RedTheme);
                break;
        }
    }
}

样式.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />


<style name="BlueTheme" parent="AppTheme.NoActionBar" >
    <item name="colorPrimary">@color/colorBluePrimary</item>
    <item name="colorPrimaryDark">@color/colorBluePrimaryDark</item>
    <item name="colorAccent">@color/colorBlueAccent</item>
</style>

<style name="RedTheme" parent="AppTheme.NoActionBar" >
    <item name="colorPrimary">@color/colorRedPrimary</item>
    <item name="colorPrimaryDark">@color/colorRedPrimaryDark</item>
    <item name="colorAccent">@color/colorRedAccent</item>
</style>

最佳答案

我觉得问题出在这个方法上

     @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked){
        Utils.changeToTheme(this,Utils.THEME_RED);
        //Assign isChecked = false;

    } else {
        Utils.changeToTheme(this, Utils.THEME_BLUE);
        // Assign isChecked = true;
    }
}

关于android - 重新创建 Activity 导致无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35135750/

相关文章:

java - 在 Android 开发中如何绘制矩阵?

java - Android SQLite ID

Android Java 服务和定时器/线程休眠

android - 谷歌地图应用程序不适用于 android

Android for Maven Eclipse 插件无法安装

android - notificationManager 通过 Id 获取通知

android - 在xml中为布局指定边框,只有左右边缘可见

java - 我如何从查询中的两个表中获取数据?

java - Android onActivityResult() 在图像捕获时返回 NullPointerException

android - KeyEvent getRepeatCount() 总是返回 0