【问题标题】:SharedPreference in non-activity-class or RecyclerView adapter class非活动类或 RecyclerView 适配器类中的 SharedPreference
【发布时间】:2018-10-08 19:46:26
【问题描述】:

我已经在设置活动中保存了共享首选项,并且能够在加载时获取它。我也能够在主要活动中获取相同的共享偏好。但是,当我试图在我打算在 recyclerview 适配器上使用的单独类上调用它时,就像每个卡片视图元素中的前缀一样,我得到一个空错误。以下是挑战:

  1. 我在设置正确的上下文时遇到了困难。
  2. 我尝试在首选项类/活动中创建一个吸气剂 从单独的 java 类调用时返回 null。
  3. 我尝试在适配器类中定义上下文并按如下方式使用它

public SharedPreferences sharedPreferences;
      public Context context;

      ...

sharedPreferences=PreferenceManager.getDefaultSharedPreferences(this.context);    
     userCurrency = sharedPreferences.getString("sPrefix","");

也试过了:

sharedPreferences=PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); 

调用此 sharedpreference 行时出现以下错误。

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference

请你帮我怎么做 要么设置一个我可以从单独的类调用的吸气剂, 或直接在这个单独的类中初始化并获取值。

主要活动

公共类 MainActivity 扩展 AppCompatActivity {

Button btnm, btnw, btnsep;
Intent intent;
TextView tvRecall;
Third third;
Shared shared;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnm = findViewById(R.id.btnmain);
    btnw = findViewById(R.id.btnwhat);
    btnsep = findViewById(R.id.sepClass);
    tvRecall=findViewById(R.id.txtRecall);

    intent = new Intent(this, Shared.class);

    btnm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            startActivity(intent);

        }
    });


    btnw.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

           SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            String s = sharedPreferences.getString("txt","");
            Log.i("testlog*****","t "+ s);
            Toast.makeText(getApplicationContext(),"Text is " + s,Toast.LENGTH_SHORT).show();

        }
    });

    btnsep.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           third = new Third();
           third.callSharedPreference();
        }
    });

}

保存共享首选项的活动

公共类 Shared 扩展 AppCompatActivity {

EditText textView ;
Button btn;
SharedPreferences sharedPreferences;
Context c;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shared);
    textView = findViewById(R.id.txtName);
    btn = findViewById(R.id.btnName);



    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            sharedPreferences.edit().putString("txt",textView.getText().toString()).apply();
        }
    });


}

public Shared () {    }

public void getlog () {
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(c);
        String s = sharedPreferences.getString("txt","");
        Log.i("testlog*****","t "+ s);
        Toast.makeText(getApplicationContext(),"Text is " + s,Toast.LENGTH_SHORT).show();
}

调用 sharedpreference 的第三类在调用时给出 null 错误

公共类第三{

Context context;
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
String s = sharedPreferences.getString("txt","");


public Third (){}

public void callSharedPreference () {

    Log.i("testlog*****","t "+ s);
    Toast.makeText(context.getApplicationContext(),"Text is ==== " + s,Toast.LENGTH_SHORT).show();

}

【问题讨论】:

  • context 对象是什么?请发布整个代码。
  • 欢迎来到 SO Salman。您是否尝试过:getDefaultSharedPreferences(context); 只有一个 context
  • 请在您的问题中添加这些代码日志并且不要将它们作为 cmets 发送。 (使用 edit 按钮)但是,请在这里查看我的答案:stackoverflow.com/questions/52686439/…
  • 你好流浪者。我终于想出在SO上发布代码。刚刚编辑了我的问题。嗨,莫森,谢谢。刚刚更新以提供代码的完整视图
  • 感谢 Mohsen 的帮助。我用你的参考弄明白了。看来我传递了错误的上下文。我仍在研究如何标记您评论中回答的问题。

标签: java android sharedpreferences


【解决方案1】:

在类 Third 中,上下文未初始化 所以你应该在构造函数方法中初始化它并在调用类时将值传递给它...... 像这样

public class Third {
Context context;
SharedPreferences sharedPreferences = 
PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
String s = sharedPreferences.getString("txt","");


public Third (Context context){
    this.context=context
}
//...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    相关资源
    最近更新 更多