我在设置活动中保存了sharedpreferences,并能够在加载时获取它。我也可以在主要活动中获取相同的共享首选项。但是,当我试图在一个打算在recyclerview适配器上使用的单独类上调用它时,就像每个卡视图元素中的前缀一样,我得到了空错误。以下是挑战:
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();
}
公共类第三{
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();
}
答案 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
}
//...
}