通过(From) AsyncTask向MainActivity发送意图



我找了几个关于堆栈溢出的问题,用于发送int到我的MainActivity并在我的TextView上显示它。但是尝试初始化活动或上下文不起作用。我得到的最新错误是:

致命异常:AsyncTask #1过程:com.dahlstore。jsonparsingdemo, PID: 32123java.lang.RuntimeException:执行时发生错误doInBackground ()android.os.AsyncTask做3.美元(AsyncTask.java: 309)在java.util.concurrent.FutureTask.finishCompletion (FutureTask.java: 354)java.util.concurrent.FutureTask.setException (FutureTask.java: 223)java.util.concurrent.FutureTask.run (FutureTask.java: 242)在android.os.AsyncTask SerialExecutor 1.美元运行(AsyncTask.java: 234)在java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java: 1113)在java.util.concurrent.ThreadPoolExecutor Worker.run美元(ThreadPoolExecutor.java: 588)java.lang.Thread.run (Thread.java: 818)原因:java.lang.NullPointerException: Attempt to invoke virtual方法的android.content.Contextandroid.app.Activity.getApplicationContext()'上的空对象参考在com.dahlstore.jsonparsingdemo.JSONTask.doInBackground (JSONTask.java: 63)在com.dahlstore.jsonparsingdemo.JSONTask.doInBackground (JSONTask.java: 21)在android.os.AsyncTask 2.美元调用(AsyncTask.java: 295)java.util.concurrent.FutureTask.run (FutureTask.java: 237)在android.os.AsyncTask SerialExecutor 1.美元运行(AsyncTask.java: 234)在java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java: 1113)在java.util.concurrent.ThreadPoolExecutor Worker.run美元(ThreadPoolExecutor.java: 588)在java.lang.Thread.run(Thread.java:818) 09-17 18:31:37.01532123 - 32152/com.dahlstore。jsonparsingdemo E/表面:getSlotFromBufferLocked:未知缓冲区:0xabea80a0

谁能解释为什么我不能发送我的意图,即使我使用活动。

/*ROW21*/      public class JSONTask extends AsyncTask<String,String, String>{
    OnDataSendToActivity dataSendToActivity;
    Activity activity;
    Intent intent;
    public JSONTask(MainActivity mainActivity) {
        dataSendToActivity = (OnDataSendToActivity)mainActivity;
    }
    public JSONTask(Activity activity){
        this.activity = activity;
    }
    @Override
    protected String doInBackground(String... params) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;
        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            InputStream stream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(stream));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
            JSONObject parentObject = new JSONObject(buffer.toString());
            JSONObject query = parentObject.getJSONObject("query").optJSONObject("results").optJSONObject("channel").optJSONObject("item");
            String temperature = query.getJSONObject("condition").optString("temp");
            String text = query.getJSONObject("condition").optString("text");
            int code = query.getJSONObject("condition").optInt("code");
           **//ROW 63**  intent = new Intent(activity.getApplicationContext(),MainActivity.class); 
            intent.putExtra("code",code);
            return temperature + " °C " +" and "+ text;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        activity.startActivity(intent);
        dataSendToActivity.sendData(result);
    }
}

MainActivity

public class MainActivity extends AppCompatActivity implements OnDataSendToActivity{
    public TextView temperatureTextView,textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        temperatureTextView = (TextView) findViewById(R.id.temperatureTextView);
        textView = (TextView) findViewById(R.id.textView);
        new JSONTask(this).execute("https://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20woeid%3D906057%20and%20u%3D%27c%27&format=json");
        Intent intent = getIntent();
        if(intent!= null) {
            int code = getIntent().getIntExtra("code", 0);
            String codeToString = String.valueOf(code);
            textView.setText(codeToString);
        } else {
            Toast.makeText(MainActivity.this, "Intent is null", Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public void sendData(String str) {
        temperatureTextView.setText(str);
    }
}

更新JSONTASK.JAVA

public class JSONTask extends AsyncTask<String,String, String>{
    OnDataSendToActivity dataSendToActivity;
    Context context;
    // single constructor to initialize both the context and dataSendToActivity
    public JSONTask(Context context){
        this.context = context;
        dataSendToActivity = (OnDataSendToActivity) ((Activity) context);
    }
    @Override
    protected String doInBackground(String... params) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;
        StringBuffer buffer = new StringBuffer();
        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            InputStream stream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(stream));
            String line = "";
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return buffer.toString();
    }
    @Override
    protected void onPostExecute(String result) {
        try {
            JSONObject parentObject = new JSONObject(result);
            JSONObject query = parentObject.getJSONObject("query").optJSONObject("results").optJSONObject("channel").optJSONObject("item");
            String temperature = query.getJSONObject("condition").optString("temp");
            String text = query.getJSONObject("condition").optString("text");
            int code = query.getJSONObject("condition").optInt("code");
            temperature += " °C " +" and "+ text;
            Intent intent = new Intent(context, MainActivity.class);
            intent.putExtra("code", code);
            context.startActivity(intent);
            if(dataSendToActivity != null){
                dataSendToActivity.sendData(temperature);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

更新MAINACTIVITY

public class MainActivity extends AppCompatActivity implements OnDataSendToActivity{
    public TextView temperatureTextView,textView;
    Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        temperatureTextView = (TextView) findViewById(R.id.temperatureTextView);
        textView = (TextView) findViewById(R.id.textView);
        intent = getIntent();
        new JSONTask(this).execute("https://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20woeid%3D906057%20and%20u%3D%27c%27&format=json");
    }

    @Override
    public void sendData(String str) {
        temperatureTextView.setText(str);
    }
}

你得到一个错误,因为你的activity是空的。这是因为您有两个构造函数。

// this is the constructor that is called
public JSONTask(MainActivity mainActivity) {
    dataSendToActivity = (OnDataSendToActivity)mainActivity;
}
// this is not called
public JSONTask(Activity activity){
    this.activity = activity;
}

所以你的activity变量永远不会初始化。

查看我的更改,

public class JSONTask extends AsyncTask<String,String, String>{
    OnDataSendToActivity dataSendToActivity;
    Context context;
    // single constructor to initialize both the context and dataSendToActivity
    public JSONTask(Context context){
        this.context = context;
        dataSendToActivity = (OnDataSendToActivity) ((Activity) context);
    }
    @Override
    protected String doInBackground(String... params) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;
        StringBuffer buffer = new StringBuffer();
        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            InputStream stream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(stream));
            String line = "";
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return buffer.toString();
    }
    @Override
    protected void onPostExecute(String result) {
        try {
            JSONObject parentObject = new JSONObject(result);
            JSONObject query = parentObject.getJSONObject("query").optJSONObject("results").optJSONObject("channel").optJSONObject("item");
            String temperature = query.getJSONObject("condition").optString("temp");
            String text = query.getJSONObject("condition").optString("text");
            int code = query.getJSONObject("condition").optInt("code");
            temperature += " °C " +" and "+ text;
            if(dataSendToActivity != null){
                dataSendToActivity.sendData(temperature, code);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

在你的MainActivity中,

public class MainActivity extends AppCompatActivity implements OnDataSendToActivity {
    public TextView temperatureTextView,textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        temperatureTextView = (TextView) findViewById(R.id.temperatureTextView);
        textView = (TextView) findViewById(R.id.textView);
        new JSONTask(this).execute("https://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20woeid%3D906057%20and%20u%3D%27c%27&format=json");
    }
    @Override
    public void sendData(String str, String code) {
        temperatureTextView.setText(str);
        textView.setText(code);
    }
}

你的OnDataSendToActivity接口将变成,

public interface OnDataSendToActivity {
    void sendData(String str, String code);
}

你真的不需要使用intent来发送你的"代码"到activity。在你的doInBackground中,把你的"code"放入一个字符串变量(你需要正确解析它),然后把这个字符串作为一个参数返回。

然后在postExecute(String result)中,变量result应该是你从doInBackground返回的值。dataSendToActivity.sendData(result)现在应该可以正常工作了

我认为错误是你在Async中使用getIntExtra。相反,使用putIntExtra将变量保存在intent中。

Put用于存储值,getIntent函数用于从intent中获取数据。

使用这一行,

intent = new Intent(getApplicationContext(),MainActivity.class); 

intent = new Intent(YourClassName.class,MainActivity.class); 

最新更新