asynctask

时间:2015-06-27 08:53:03

标签: android android-asynctask

如果我想从其他活动访问asynctask中的textview,则在asynctask中未定义此textview。我该怎么办?

例如此代码:

     private class ParserTask extends
        AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {

    // Parsing the data in non-ui thread
    @Override
    protected List<List<HashMap<String, String>>> doInBackground(
            String... jsonData) {
          // some code
              }

    @Override
    protected void onPostExecute(List<List<HashMap<String, String>>> result) {
        // some code

        *   tvDistanceDuration.setText("distanse:");

            saveLine(points);
        }
}
public void saveLine(final ArrayList<LatLng> points) {

    //// Inflate the menu; this adds items to the action bar if it is   present.

*   findViewById(R.id.button1).setVisibility(View.VISIBLE);
            //// some code
    }

错误发生在*行

1 个答案:

答案 0 :(得分:0)

使用textview传递包含视图的活动并执行activity.findViewById()

**不要在doInBackgroundMethod中执行此操作,因为您将获得NullPointer异常。

public class MyAsyncTask extends AsyncTask<Object, Void, String> {
private Activity mActivity;

public MyAsyncTask(Activity activity) {
    super();
    this.mActivity = activity;
}

@Override
protected void onPreExecute() {
    mActivity.findViewById(R.id.textview);
}

@Override
protected String doInBackground(Object... params) {

    return "ok";
}

@Override
protected void onPostExecute(String result) {

}
}