gpt4 book ai didi

java - 按下后退按钮时 SwitchCompat 状态发生变化

转载 作者:行者123 更新时间:2023-12-01 11:58:44 25 4
gpt4 key购买 nike

我在 ActionBar 中有一个 SwitchCompat 小部件。

当按下后退按钮并“重新打开”应用程序时,SwitchCompat 会丢失状态,从打开变为关闭。

我正在实现前台,但这并不能阻止 SwitchCompat 更改状态。

我尝试使用 SharedPreferences 但应用程序崩溃了。

MainActivity.java

SwitchCompat switchService;

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

switchService = (SwitchCompat) findViewById(R.id.toggleButton);

LoadPreferences();
}

private void SavePreferences(){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("state", switchService.isEnabled());
editor.apply(); // I missed to save the data to preference here,.
}

private void LoadPreferences(){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
Boolean state = sharedPreferences.getBoolean("state", false);
switchService.setEnabled(state);
}

@Override
public void onBackPressed() {
SavePreferences();
super.onBackPressed();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.toggle_ButtonL);
MenuItemCompat.getActionView(item).findViewById(R.id.toggleButton);
switchService = (SwitchCompat) MenuItemCompat.getActionView(item);
switchService.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if (isChecked) {
// Creates SMSMonitorService intent
Intent SMSMonitorIntent = new Intent(MainActivity.this, SMSMonitorService.class);
// Start SMSMonitorService
startService(SMSMonitorIntent);
Log.i(DEBUG_TAG, "SMSMonitor Started");

} else {
// Creates SMSMonitorService intent
Intent SMSMonitorIntent = new Intent(MainActivity.this, SMSMonitorService.class);
// Stop SMSMonitorService
stopService(SMSMonitorIntent);
Log.i(DEBUG_TAG,"SMSMonitor Stopped");
Toast.makeText(getApplicationContext(), getResources().getString(R.string.toggle_off), Toast.LENGTH_SHORT).show();
}
}
});

return true;
}

日志猫:

01-25 23:41:07.995    1341-1341/com.test.wikitext E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.test.wikitext, PID: 1341
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.wikitext/com.test.wikitext.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SwitchCompat.setEnabled(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SwitchCompat.setEnabled(boolean)' on a null object reference
at com.test.wikitext.MainActivity.LoadPreferences(MainActivity.java:249)
at com.test.wikitext.MainActivity.onCreate(MainActivity.java:73)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)

(继续)

我如何保存 SwitchCompat 的状态,当按下后退按钮时,不会丢失其当前状态?

最佳答案

我知道我参加聚会迟到了,但这可能对其他人有帮助

嗨,和你一样,我一直在寻找这个问题的答案,答案就在你的问题中,只需替换如下代码即可

在 OnCreateOptionsMenu 中更改代码

MenuItem item = menu.findItem(R.id.toggle_ButtonL);
MenuItemCompat.getActionView(item).findViewById(R.id.toggleButton);
switchService = (SwitchCompat) MenuItemCompat.getActionView(item);

MenuItem item = menu.findItem(R.id.toggle_ButtonL);
switchService = (SwitchCompat)MenuItemCompat.getActionView(item).findViewById(R.id.toggleButton);

这样就可以了。现在我们可以摆脱 NullPointerException

关于java - 按下后退按钮时 SwitchCompat 状态发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28145542/

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