Android 本地布尔值未注册

Android 本地布尔值未注册,android,boolean,Android,Boolean,我目前正在尝试添加一个关于“停止”首选项的简单警报对话框。有点像一个停止按钮 我在管理本地和公共布尔人方面遇到了麻烦。我使用SharedReferences跨活动发送设置StopSetting,而不实际打开其他活动 然而,当我试图编辑布尔值“StopTrue”时,问题就出现了。当我在代码中的任意位置设置一个默认值,空布尔值StopTrue,然后尝试使用共享首选项编辑器编辑布尔值时,这两个变量被注册为单独的变量。因此,空白变量被注册为未使用的,并且编辑的变量被设置为“总是true”或“总是fals

我目前正在尝试添加一个关于“停止”首选项的简单警报对话框。有点像一个停止按钮

我在管理本地和公共布尔人方面遇到了麻烦。我使用SharedReferences跨活动发送设置StopSetting,而不实际打开其他活动

然而,当我试图编辑布尔值“StopTrue”时,问题就出现了。当我在代码中的任意位置设置一个默认值,空布尔值StopTrue,然后尝试使用共享首选项编辑器编辑布尔值时,这两个变量被注册为单独的变量。因此,空白变量被注册为未使用的,并且编辑的变量被设置为“总是true”或“总是false”。 有人知道这里有什么问题吗? 代码如下:

final Button StopButton = (Button) findViewById(R.id.StopButton);
        StopButton.setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            public void onClick(View v) {
                //Creates Dialog
                AlertDialog.Builder StopDialog = new AlertDialog.Builder(MainActivity.this);
                StopDialog.setTitle(R.string.Stop_Title);
                StopDialog.setMessage(R.string.Stop_Message);
                StopDialog.setPositiveButton(R.string.Yes_Button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        boolean StopTrue = true;
                        SharedPreferences StopSetting = getSharedPreferences("StopSetting", MODE_PRIVATE);
                        SharedPreferences.Editor StopEditor = StopSetting.edit();
                        StopEditor.putBoolean("StopSetting", StopTrue);
                        StopEditor.apply();
                        //Closes box
                        finish();
                    }
                });
                StopDialog.setNegativeButton(R.string.No_Button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        boolean StopTrue = false;
                        SharedPreferences StopSetting = getSharedPreferences("StopSetting", MODE_PRIVATE);
                        SharedPreferences.Editor StopEditor = StopSetting.edit();
                        StopEditor.putBoolean("StopSetting", StopTrue);
                        StopEditor.apply();
                        //Closes box
                        finish();
                    }
                });
                StopDialog.setNeutralButton(R.string.Negative_Button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //Closes box
                        finish();
                    }
                });
            }
        });

将StopTrue声明为全局变量,并仅在
onClick
methods

//declare this as global variable
Use boolean StopTrue; 
 @Override
public void onClick(DialogInterface dialog, int id) {
  StopTrue = true;
  .....

}

@Override
public void onClick(DialogInterface dialog, int which) {
   StopTrue = false;
   .......
}
否则,对于每个onClick实例,StopTrue变量将具有相应的赋值