我有一个专门用于管理共享偏好的课程,但是我一直在我的一个电话上获得NPE。继续上课,
public class UserManager extends Activity{
Context mContext;
UserManager(Context mContext){
this.mContext = mContext;
}
public boolean getFromMatches(String userUid, String matchUid){
SharedPreferences spMatches = mContext.getSharedPreferences(getString(R.string.match_key), Context.MODE_PRIVATE);
if(spMatches.contains(userUid + matchUid)){
return true;
}else{
return false;
}
}
}
这是我的主要活动传递的内容:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
userManager = new UserManager(getApplicationContext());
}
此行收到NPE:
SharedPreferences spMatches = mContext.getSharedPreferences(getString(R.string.match_key), Context.MODE_PRIVATE);
答案 0 :(得分:0)
使用此
SharedPreferences spMatches = mContext.getSharedPreferences(getResources().getString(R.string.match_key), Context.MODE_PRIVATE);
答案 1 :(得分:0)
用此
替换getFromMatches
函数
public boolean getFromMatches(String userUid, String matchUid){
SharedPreferences spMatches = mContext.getSharedPreferences(mContext.getString(R.string.match_key), Context.MODE_PRIVATE);
if(spMatches.contains(userUid + matchUid)){
return true;
}else{
return false;
}
}
用于调用getString
或您需要的任何资源Context