Android 异步任务运行错误

Android 异步任务运行错误,android,android-asynctask,Android,Android Asynctask,我尝试使用asynctask加载listview数据。当第一个活动调用启动新活动时,它应该加载新活动并显示数据。但是我的第二项活动没有开始 我的第二节课是 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.se

我尝试使用asynctask加载listview数据。当第一个活动调用启动新活动时,它应该加载新活动并显示数据。但是我的第二项活动没有开始

我的第二节课是

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_data);
    search_data=getIntent().getExtras().getString("Search_Value");

    list = (ListView) findViewById(R.id.listView1);

    accessWebService();

}

class SearchData extends AsyncTask<String, String, String>{


    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub

        List<NameValuePair> params = new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("rest_name", search_data));

        Log.d("request!", "starting");

        JSONObject json = jsonParser.makeHttpRequest(Search, "POST", params);

        Log.d("Search", json.toString());

        JSONArray jsonMainNode = json.optJSONArray("Search");

        for (int i = 0; i < jsonMainNode.length(); i++) {
        JSONObject jsonChildNode;
        try {

            jsonChildNode = jsonMainNode.getJSONObject(i);
            String restName = jsonChildNode.optString("name");
            //Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
            String address = jsonChildNode.optString("address");
            String mobile = jsonChildNode.optString("mobile");
            String direction = "Direction: "+jsonChildNode.optString("direction");
            String bestTime = "Best time to visite: "+jsonChildNode.optString("bestTime");
            String food = jsonChildNode.optString("food");
            String dress = jsonChildNode.optString("dress");
            String priceRange = "Price Range: "+jsonChildNode.optString("priceRange");

            Log.d("Dress", dress);

            listData = new ArrayList<String>();
            listData.add(restName);
            listData.add(address);
            listData.add(mobile);
            listData.add(direction);
            listData.add(bestTime);
            listData.add(food);
            listData.add(dress);
            listData.add(priceRange);

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        MyListAdapter adapter = new MyListAdapter(Search.this,listData);
        list.setAdapter(adapter);



        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
    }



}

public void accessWebService() {
    SearchData data = new SearchData();
    // passes values for the urls string array
    data.execute();
}

有人能帮我解决这个问题吗?

将这些行移动到
onPostExecute
,因为
doInBackGround
将在非UI线程中运行

MyListAdapter adapter = new MyListAdapter(Search.this,listData);
list.setAdapter(adapter);

将这些行移动到
onPostExecute
,因为
doInBackGround
将在非UI线程中运行

MyListAdapter adapter = new MyListAdapter(Search.this,listData);
list.setAdapter(adapter);

将这些行移动到
onPostExecute
,因为
doInBackGround
将在非UI线程中运行

MyListAdapter adapter = new MyListAdapter(Search.this,listData);
list.setAdapter(adapter);

将这些行移动到
onPostExecute
,因为
doInBackGround
将在非UI线程中运行

MyListAdapter adapter = new MyListAdapter(Search.this,listData);
list.setAdapter(adapter);
将其移动到onPostExecute()

这是因为您无法在doInbackGround方法中更新UI

要更新UI,必须在onPostExecute方法中更新它

在doInbackGround中,您可以执行网络UI工作。

将此移动到onPostExecute()

这是因为您无法在doInbackGround方法中更新UI

要更新UI,必须在onPostExecute方法中更新它

在doInbackGround中,您可以执行网络UI工作。

将此移动到onPostExecute()

这是因为您无法在doInbackGround方法中更新UI

要更新UI,必须在onPostExecute方法中更新它

在doInbackGround中,您可以执行网络UI工作。

将此移动到onPostExecute()

这是因为您无法在doInbackGround方法中更新UI

要更新UI,必须在onPostExecute方法中更新它


在doInbackGround中,您可以执行网络UI工作。

MyListAdapter似乎有问题

MyListAdapter adapter = new MyListAdapter(Search.this,listData);
list.setAdapter(adapter);
您需要从onPostExecute调用runOnUiThread()

      runOnUiThread(new Runnable() {

          @Override
                public void run() {
                   MyListAdapter adapter = new MyListAdapter(Search.this,listData);
                   list.setAdapter(adapter);
                }
          });

我的iListAdapter似乎有问题

MyListAdapter adapter = new MyListAdapter(Search.this,listData);
list.setAdapter(adapter);
您需要从onPostExecute调用runOnUiThread()

      runOnUiThread(new Runnable() {

          @Override
                public void run() {
                   MyListAdapter adapter = new MyListAdapter(Search.this,listData);
                   list.setAdapter(adapter);
                }
          });

我的iListAdapter似乎有问题

MyListAdapter adapter = new MyListAdapter(Search.this,listData);
list.setAdapter(adapter);
您需要从onPostExecute调用runOnUiThread()

      runOnUiThread(new Runnable() {

          @Override
                public void run() {
                   MyListAdapter adapter = new MyListAdapter(Search.this,listData);
                   list.setAdapter(adapter);
                }
          });

我的iListAdapter似乎有问题

MyListAdapter adapter = new MyListAdapter(Search.this,listData);
list.setAdapter(adapter);
您需要从onPostExecute调用runOnUiThread()

      runOnUiThread(new Runnable() {

          @Override
                public void run() {
                   MyListAdapter adapter = new MyListAdapter(Search.this,listData);
                   list.setAdapter(adapter);
                }
          });

请粘贴json响应字符串请粘贴json响应字符串请粘贴json响应字符串请粘贴json响应字符串请粘贴json响应字符串