非活动类或RecyclerView适配器类中的SharedPreference

时间:2018-10-08 19:46:27

标签: java android sharedpreferences

我在设置活动中保存了sharedpreferences,并能够在加载时获取它。我也可以在主要活动中获取相同的共享首选项。但是,当我试图在一个打算在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

请您能帮我怎样 要么设置一个我可以从一个单独的类中调用的getter, 或直接初始化并在此单独的类中获取值。

主要活动

公共类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时,在调用时会给出上述空错误

公共类第三{

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

}

1 个答案:

答案 0 :(得分:0)

在第三类中,上下文未初始化 因此,您应该在构造函数方法中对其进行初始化,并在调用该类时将值传递给它。 像这样

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


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