小编MNT*_*MNT的帖子

无法从静态上下文引用非静态方法'getSharedPreferences(java.lang.String,int)'

我有一个应用程序,我试图将按钮点击次数限制为五次,然后一旦用户按下此按钮五次,它应该禁用.

但是我收到了上述错误,我不知道为什么.

有任何想法吗 ?

          buttonadd.setOnClickListener(new OnClickListener () {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), MainActivity3.class);
            startActivity(intent);

            int clicks = 0;
            clicks++;

            if (clicks >= 5){
                buttonadd.setEnabled(false);
            }

            SharedPreferences prefs = Context.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putInt("clicks", clicks);
            editor.apply();

        }

    });
Run Code Online (Sandbox Code Playgroud)

java android android-button android-view android-studio

4
推荐指数
1
解决办法
3389
查看次数