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

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;
        }
    }
}


Styles.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;
    }
}

09-30 18:26
查看更多