用于分析数据的java getBaseContext()错误
此代码用于尝试注册。类安卓 studio应用程序,用于解析数据。我的代码在最后的getBaseContext()代码下不断给我错误,我不知道为什么,因为我认为它看起来不错!!!该代码是为一个实验室,所以它应该是正确的,但我不断得到错误
谁能告诉我??任何帮助都将不胜感激!多谢各位
public class AttemptRegistration extends
AsyncTask<String, Integer, String> {
int success;
String message = " ";
@Override
protected String doInBackground(String... args) {
try {
Map<String, String> params = new HashMap<String, String>();
params.put("tag", "register");
params.put("username", args[0]);
params.put("password", args[1]);
params.put("email", args[2]);
//
HttpUtility.sendPostRequest(params);
//
String response = HttpUtility.readRespone();
JSONObject jObj = null;
try {
jObj = new JSONObject(response);
success = jObj.getInt("success");
message = jObj.getString("message");
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data" + e.toString());
}
} catch (IOException ex) {
ex.printStackTrace();
}
HttpUtility.disconnect();
return message;
}
protected void onPostExecute(String status) {
if (status !=null) {
Toast.makeText(getBaseContext(), status, Toast.LENGTH_LONG).show();
if(success == 1) {
startActivity (new Intent(getBaseContext(), LoginActivity.class));
}
}
}
# 1 楼答案
getBaseContext()是
ContextWrapper
的一种方法。由于您得到了cannot resolve the method
错误,这意味着AsyncTask
类没有定义为继承自ContextWrapper
(Activity
)的类的内部类。您可以将Context
传递给您的AsyncTask
然后用
mContext
代替getBaseContext()