【问题标题】:Non-static method 'getSharedPreferences (java.lang.String, int)' cannot be referenced from a static context不能从静态上下文中引用非静态方法“getSharedPreferences (java.lang.String, int)”
【发布时间】:2015-06-07 17:29:36
【问题描述】:

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

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

有什么想法吗?

          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();

        }

    });

【问题讨论】:

    标签: java android android-studio android-view android-button


    【解决方案1】:

    正如错误消息所说,getSharedPreferences() 是一种非静态方法。当您执行Context.getSharedPreferences(...) 时,您正试图直接从课堂上调用它。相反,您需要从 Context 实例调用它。

    如果您的代码在 Activity 内(如 Activity 扩展 Context),您可以这样做:

    SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
    

    【讨论】:

      【解决方案2】:

      这意味着您需要一个Context 对象的实例来调用getSharedPreferences() 方法。如果你在 Activity 内,试试这个:

      this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE)
      

      【讨论】:

      • 成功了,谢谢。然而,当我测试它时,它仍然让用户按下按钮超过 5 次,我不知道为什么......
      • 无需致电getApplicationContext()。只需拨打getSharedPreferences() 即可。
      • 如果是RecyclerView的Adapter类呢?我们如何实现 Sharedpreference ?感谢提前
      【解决方案3】:

      您错误地尝试以 static 方式使用 virtual 方法 getSharedPreferences(),这就是它给出编译时错误的原因。

      如果该代码在 Activity 中,则替换

      Context.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
      

      getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
      

      如果是Fragment,使用

      getActivity().getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
      

      编辑:

      使用

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

      并使clicks 成为类成员,即将其声明为

      private int clicks;
      

      Activity

      编辑 2:

      我想我已经理解你所犯的错误了。在您的代码中,替换

      int clicks = 0;
      

      SharedPreferences prefs = getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
      int clicks = prefs.getInt("clicks", 0);
      

      试试这个。应该这样做。

      【讨论】:

      • 谢谢,我已经改变了它并且它的工作。但是,当我测试它时,它仍然让我点击按钮超过 5 次......
      • 到现在,我会告诉你的。
      • 它仍然允许点击按钮超过 5 次。
      • clicks 始终为 1,因为每次单击后,您都会创建一个新的 clicks,并将其设置为 0,然后将其递增为 1。如果将 clicks 变量移至该类,它将起作用。没有必要存储它。你需要回顾一下你对变量作用域和现存变量的理解。
      • @pzulw:虽然你是对的,但我认为他想要的是按钮应该可以点击 5 次 PERIOD,这就是他使用SharedPreference 的原因。我建议 EDIT 2 使其正常工作:)
      猜你喜欢
      • 2020-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多