有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

用于分析数据的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) 个答案

  1. # 1 楼答案

    .My code keeps giving me errors under my getBaseContext() code at the end and I have no idea why as I think it looks ok!!!

    getBaseContext()ContextWrapper的一种方法。由于您得到了cannot resolve the method错误,这意味着AsyncTask类没有定义为继承自ContextWrapperActivity)的类的内部类。您可以将Context传递给您的AsyncTask

    public class AttemptRegistration extends AsyncTask<String, Integer, String> {
        private final Context mContext;
        public AttemptRegistration(Context context) {
           mContext = context;
        }
    

    然后用mContext代替getBaseContext()