我已在设置 Activity 中保存了共享首选项,并且能够在加载时获取它。我还可以在主要 Activity 中获取相同的共享首选项。然而,当我尝试在一个单独的类上调用它时,我打算在 recyclerview 适配器上使用它,就像每个卡片 View 元素中的前缀一样,我收到一个 null 错误。以下是挑战:
- 我在建立正确的上下文方面遇到了困难。
- 我尝试在首选项类/Activity 中创建一个 getter 从单独的 java 类调用时返回 null。
- 我尝试在适配器类中定义上下文并按如下方式使用它
public SharedPreferences sharedPreferences;
public Context context;
...
sharedPreferences=PreferenceManager.getDefaultSharedPreferences(this.context);
userCurrency = sharedPreferences.getString("sPrefix","");
还尝试过:
sharedPreferences=PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
调用此共享首选项行时出现以下错误。
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
请你帮我解决一下 要么设置一个我可以从单独的类调用的 setter/getter , 或者直接在这个单独的类中初始化并获取值。
主要 Activity
公共(public)类 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();
}
});
}
保存共享首选项的 Activity
公共(public)类 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();
}
第三类调用共享首选项,在调用时给出如上所述的 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();
}
最佳答案
在第三类中,上下文未初始化 所以你应该在构造函数方法中初始化它,并在调用该类时将值传递给它...... 像这样
public class Third {
Context context;
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
String s = sharedPreferences.getString("txt","");
public Third (Context context){
this.context=context
}
//...
}
关于java - 非 Activity 类或 RecyclerView 适配器类中的 SharedPreference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52709112/