我正在尝试更改应用的主题,因此当用户按下按钮时,我会调用以下方法:
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>
答案 0 :(得分:1)
我认为问题在于这个方法
@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;
}
}