Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用SharedReferences存储每次用户单击按钮时递增的int&;下次启动应用程序时检索值_Android - Fatal编程技术网

Android 如何使用SharedReferences存储每次用户单击按钮时递增的int&;下次启动应用程序时检索值

Android 如何使用SharedReferences存储每次用户单击按钮时递增的int&;下次启动应用程序时检索值,android,Android,我是Android开发的初学者,我一直在为学校开发一款非常简单的投票应用程序 基本上,我在多个活动中创建了对象,这些对象充当int计数器来记录各种帖子的投票(以点击的形式)。我已经做到了这一点,并且能够查看不同活动中所有计数器的结果。此外,该活动会自动切换到投票系统中下一个位置的新活动 我意识到,如果应用程序被终止或崩溃,我将丢失对象(计数器)的所有数据/投票。因此,我尝试实现共享首选项,以便在单击按钮时存储和更新投票计数 但是,当我使用代码将共享首选项变量重新指定为计数器对象值时,我的应用程序

我是Android开发的初学者,我一直在为学校开发一款非常简单的投票应用程序

基本上,我在多个活动中创建了对象,这些对象充当int计数器来记录各种帖子的投票(以点击的形式)。我已经做到了这一点,并且能够查看不同活动中所有计数器的结果。此外,该活动会自动切换到投票系统中下一个位置的新活动

我意识到,如果应用程序被终止或崩溃,我将丢失对象(计数器)的所有数据/投票。因此,我尝试实现共享首选项,以便在单击按钮时存储和更新投票计数

但是,当我使用代码将共享首选项变量重新指定为计数器对象值时,我的应用程序崩溃

我似乎无法理解修复代码的内容和位置,以便计数器从上一个值恢复。下面是我的代码:

public class SPL extends AppCompatActivity {

SharedPreferences retrieve = getApplicationContext().getSharedPreferences("Result", MODE_PRIVATE);

public int spl1count = retrieve.getInt("HB1", 0);
public int spl2count = retrieve.getInt("HB2", 0);

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

    final Button spl1vb = (Button) findViewById(R.id.spl1vb);
    final Button spl2vb = (Button) findViewById(R.id.spl2vb);


    spl1vb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            spl1count ++;
            SharedPreferences sharedPref = getSharedPreferences("Result", Context.MODE_PRIVATE);

            SharedPreferences.Editor editor = sharedPref.edit();

            editor.putInt("HB1", spl1count);
            editor.apply();
            Toast.makeText(getApplicationContext(),"Saved ", Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(getApplicationContext(), ASPL.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();
        }
    });


    spl2vb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            spl2count ++;
            SharedPreferences sharedPref = getSharedPreferences("Result", Context.MODE_PRIVATE);

            SharedPreferences.Editor editor = sharedPref.edit();

            editor.putInt("HB2", spl2count);
            editor.apply();
            Toast.makeText(getApplicationContext(),"Saved ", Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(getApplicationContext(), ASPL.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();
        }
    });


}
该应用程序崩溃,在查看日志后,它告诉我,在我使用共享首选项“检索”以获取旧数据以放入计数器的线路上有一个错误

如果我删除此项,当再次启动时,计数器将设置回0,我认为这是因为计数器实际上没有设置为共享首选项值

以下是我在另一个活动中收集共享偏好结果的代码:

  SharedPreferences sharedPref = getSharedPreferences("Result", Context.MODE_PRIVATE);

    final int spl1r = sharedPref.getInt("HB1", 0);
    final int spl2r = sharedPref.getInt("HB2", 0);
然后,我使用一个警报对话框来显示spl1r和spl2r

我花了几个小时在互联网上寻找解决方案,但没有一个对我有效。任何帮助都将不胜感激。提前谢谢


因此,我根据Evin的答案更新了代码,但我的代码仍然崩溃,这次是在
OnCreate

这是我的新代码:

     public class SPL extends AppCompatActivity {


SharedPreferences retrieve;
public int spl1count;
public int spl2count;

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

    retrieve = getApplicationContext().getSharedPreferences("Result", MODE_PRIVATE);
    spl1count = retrieve.getInt("HB1", 0);
    spl2count = retrieve.getInt("HB2", 0);

     Button spl1vb = (Button) findViewById(R.id.spl1vb);
     Button spl2vb = (Button) findViewById(R.id.spl2vb);



    spl1vb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            spl1count ++;
            SharedPreferences sharedPref = getSharedPreferences("Result", Context.MODE_PRIVATE);

            SharedPreferences.Editor editor = sharedPref.edit();

            editor.putInt("HB1", spl1count);
            editor.apply();
            Toast.makeText(getApplicationContext(),"Saved ", Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(getApplicationContext(), ASPL.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();
        }
    });


    spl2vb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            spl2count ++;
            SharedPreferences sharedPref = getSharedPreferences("Result", Context.MODE_PRIVATE);

            SharedPreferences.Editor editor = sharedPref.edit();

            editor.putInt("HB2", spl2count);
            editor.apply();
            Toast.makeText(getApplicationContext(),"Saved ", Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(getApplicationContext(), ASPL.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();
        }
    });


}

我还在做错什么吗?谢谢。

将这些行放在
onCreate
中:

SharedPreferences retrieve = getApplicationContext().getSharedPreferences("Result", MODE_PRIVATE);

public int spl1count = retrieve.getInt("HB1", 0);
public int spl2count = retrieve.getInt("HB2", 0);
当它们还没有引用任何Android上下文(在活动/应用程序的
onCreate
方法中构建)时,您正在初始化它们,这就是为什么ApplicationContext的getSharedReferences方法将是空引用的原因


如果仍然希望将这些值声明为成员变量,则类的外观类似(拆分声明和赋值):


你的意思是我应该将其添加到我正在检索的活动的OnCreate中以用于结果警报框,或者添加到我有计数器的活动中?您在其中声明这些对象的活动中,我编辑了我的问题,请检查一下。是的,我按照您告诉我的方式添加了它,但是应用程序仍然崩溃,并且在OnCreate now中给了我一个错误…你能粘贴堆栈跟踪吗?天哪,它现在可以工作了。非常感谢你,你是一个救命恩人。非常感谢:D
public class SPL extends AppCompatActivity {
    SharedPreferences retrieve;
    public int spl1count;
    public int spl2count;

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

        retrieve = getApplicationContext().getSharedPreferences("Result", MODE_PRIVATE);
        spl1count = retrieve.getInt("HB1", 0);
        spl2count = retrieve.getInt("HB2", 0);

        final Button spl1vb = (Button) findViewById(R.id.btn1);
        final Button spl2vb = (Button) findViewById(R.id.btn2);

       /* Your listeners */
    }
}