我正在尝试将一个字符串保存在SharedPreferences中的AsyncTask中,但不幸的是,我得到了以下错误:
android.content.Context.getSharedPreferences(java.lang.String,:尝试在空对象引用上调用虚拟方法“android.content.SharedPreferences android.content.SharedPreferences int)”
以下是相关代码:
public class FetchData extends AsyncTask<Void, Void, String> {
private Context context;
String deviceId;
private AsyncInterface asyncInterface;
public FetchData(AsyncInterface asyncInterface) {
this.context = context;
this.asyncInterface = asyncInterface;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
SharedPreferences sharedPreferences = context.getSharedPreferences("deviceId", MODE_PRIVATE); //Error here
deviceId = sharedPreferences.getString("deviceId", deviceId);
}
}我做错什么了?
发布于 2018-04-04 19:13:47
也许您应该添加一个构造函数参数Context context。
https://stackoverflow.com/questions/49658604
复制相似问题