执行时AsynTask强制关闭

时间:2015-05-07 03:47:13

标签: android listview android-asynctask

请帮忙...... 我是asyntask的新手 所以我真的不明白哪些参数传递给哪一个

        private class LoadViewTask extends AsyncTask<Void, HashMap<String, String>, Void> {
    private SearchListAdapter adapter;
    // Before running code in separate thread
    @Override
    protected void onPreExecute() {

    }

    // The code to be executed in a background thread.
    @Override
    protected Void doInBackground(Void... params) {

            String filterRev = "";
            try {
                filterRev = URLEncoder.encode(
                        MainActivity.FILTER_SEARCH_ENTERPRISE, "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            final String dummyURL5star = MainActivity.BASE_URL
                    + "v_enterprise-select5star.php?filteritem=" + filterRev;
            final String dummyURL4star = MainActivity.BASE_URL
                    + "v_enterprise-select4star.php?filteritem=" + filterRev;
            final String dummyURL3star = MainActivity.BASE_URL
                    + "v_enterprise-select3star.php?filteritem=" + filterRev;
            final String dummyURL2star = MainActivity.BASE_URL
                    + "v_enterprise-select2star.php?filteritem=" + filterRev;
            final String dummyURL1star = MainActivity.BASE_URL
                    + "v_enterprise-select1star.php?filteritem=" + filterRev;

            xResult5star = MainActivity.getRequestSelect(dummyURL5star,
                    SearchActivity.this);


            try {
                jObject5 = new JSONObject(xResult5star);
            JSONArray menuitemArray = jObject5.getJSONArray("venterprise");
            if (panjangList < menuitemArray.length()) {
                for (int i = 0; i < panjangList; i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(KEY_IDENTERPRISE, menuitemArray.getJSONObject(i)
                            .getString("identerprise").toString());
                    map.put(KEY_NAMAENTERPRISE, menuitemArray.getJSONObject(i)
                            .getString("nama").toString());
                    map.put(KEY_ALAMATENTERPRISE, menuitemArray.getJSONObject(i)
                            .getString("address").toString());
                    map.put(KEY_KATEGORI,
                            menuitemArray.getJSONObject(i).getString("linkkat")
                                    .toString());
                    map.put(KEY_LINK,
                            menuitemArray.getJSONObject(i).getString("linksub")
                                    .toString());
                    map.put(KEY_KAT,
                            menuitemArray.getJSONObject(i).getString("kategori")
                                    .toString());
                    map.put(KEY_SUB,
                            menuitemArray.getJSONObject(i).getString("subkategori")
                                    .toString());
                    map.put(KEY_IDKAT,
                            menuitemArray.getJSONObject(i).getString("idkategori")
                                    .toString());
                    map.put(KEY_IDSUB,
                            menuitemArray.getJSONObject(i)
                                    .getString("idsubkategori").toString());
                    map.put(KEY_THUMB_URL, menuitemArray.getJSONObject(i)
                            .getString("picture").toString());
                    map.put(KEY_HITS,
                            menuitemArray.getJSONObject(i).getString("hit")
                                    .toString());
                    map.put(KEY_NOKONTRAKS, menuitemArray.getJSONObject(i)
                            .getString("nokontrak").toString());
                    map.put(KEY_STAR, "5");
                    listRow.add(map);
                }
            }

                /*
                 * list = (ListView) findViewById(R.id.lvRowsEnterpriseSearch);
                 * adapter = new SearchListAdapter(this, listRow);
                 * list.setAdapter(adapter); registerForContextMenu(list);
                 */
             else {
                sisaList = panjangList - menuitemArray.length();
                for (int i = 0; i < menuitemArray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(KEY_IDENTERPRISE, menuitemArray.getJSONObject(i)
                            .getString("identerprise").toString());
                    map.put(KEY_NAMAENTERPRISE, menuitemArray.getJSONObject(i)
                            .getString("nama").toString());
                    map.put(KEY_ALAMATENTERPRISE, menuitemArray.getJSONObject(i)
                            .getString("address").toString());
                    map.put(KEY_KATEGORI,
                            menuitemArray.getJSONObject(i).getString("linkkat")
                                    .toString());
                    map.put(KEY_LINK,
                            menuitemArray.getJSONObject(i).getString("linksub")
                                    .toString());
                    map.put(KEY_KAT,
                            menuitemArray.getJSONObject(i).getString("kategori")
                                    .toString());
                    map.put(KEY_SUB,
                            menuitemArray.getJSONObject(i).getString("subkategori")
                                    .toString());
                    map.put(KEY_IDKAT,
                            menuitemArray.getJSONObject(i).getString("idkategori")
                                    .toString());
                    map.put(KEY_IDSUB,
                            menuitemArray.getJSONObject(i)
                                    .getString("idsubkategori").toString());
                    map.put(KEY_THUMB_URL, menuitemArray.getJSONObject(i)
                            .getString("picture").toString());
                    map.put(KEY_HITS,
                            menuitemArray.getJSONObject(i).getString("hit")
                                    .toString());
                    map.put(KEY_NOKONTRAKS, menuitemArray.getJSONObject(i)
                            .getString("nokontrak").toString());
                    map.put(KEY_STAR, "5");
                    listRow.add(map);
                }
                /*
                 * list = (ListView) findViewById(R.id.lvRowsEnterpriseSearch);
                 * adapter = new SearchListAdapter(this, listRow);
                 * list.setAdapter(adapter); registerForContextMenu(list);
                 */

                xResult4star = MainActivity.getRequestSelect(dummyURL4star,
                        SearchActivity.this);
                jObject4 = new JSONObject(xResult4star);
                JSONArray menuitemArray4 = jObject4.getJSONArray("venterprise");
                JSONArray menuitemArraySession = jObject4.getJSONArray("session");

                if (sisaList < menuitemArray4.length()) {
                    for (int i = 0; i < sisaList; i++) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put(KEY_IDENTERPRISE, menuitemArray4.getJSONObject(i)
                                .getString("identerprise").toString());
                        map.put(KEY_NAMAENTERPRISE, menuitemArray4.getJSONObject(i)
                                .getString("nama").toString());
                        map.put(KEY_ALAMATENTERPRISE,
                                menuitemArray4.getJSONObject(i)
                                        .getString("address").toString());
                        map.put(KEY_KATEGORI, menuitemArray4.getJSONObject(i)
                                .getString("linkkat").toString());
                        map.put(KEY_LINK, menuitemArray4.getJSONObject(i)
                                .getString("linksub").toString());
                        map.put(KEY_KAT,
                                menuitemArray4.getJSONObject(i)
                                        .getString("kategori").toString());
                        map.put(KEY_SUB,
                                menuitemArray4.getJSONObject(i)
                                        .getString("subkategori").toString());
                        map.put(KEY_IDKAT, menuitemArray4.getJSONObject(i)
                                .getString("idkategori").toString());
                        map.put(KEY_IDSUB, menuitemArray4.getJSONObject(i)
                                .getString("idsubkategori").toString());
                        map.put(KEY_THUMB_URL, menuitemArray4.getJSONObject(i)
                                .getString("picture").toString());
                        map.put(KEY_HITS, menuitemArray4.getJSONObject(i)
                                .getString("hit").toString());
                        map.put(KEY_NOKONTRAKS, menuitemArray4.getJSONObject(i)
                                .getString("nokontrak").toString());
                        map.put(KEY_STAR, "4");
                        listRow.add(map);
                    }
                    /*
                     * list = (ListView) findViewById(R.id.lvRowsEnterpriseSearch);
                     * adapter = new SearchListAdapter(this, listRow);
                     * list.setAdapter(adapter); registerForContextMenu(list);
                     */
                } else {

                    for (int i = 0; i < menuitemArray4.length(); i++) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put(KEY_IDENTERPRISE, menuitemArray4.getJSONObject(i)
                                .getString("identerprise").toString());
                        map.put(KEY_NAMAENTERPRISE, menuitemArray4.getJSONObject(i)
                                .getString("nama").toString());
                        map.put(KEY_ALAMATENTERPRISE,
                                menuitemArray4.getJSONObject(i)
                                        .getString("address").toString());
                        map.put(KEY_KATEGORI, menuitemArray4.getJSONObject(i)
                                .getString("linkkat").toString());
                        map.put(KEY_LINK, menuitemArray4.getJSONObject(i)
                                .getString("linksub").toString());
                        map.put(KEY_KAT,
                                menuitemArray4.getJSONObject(i)
                                        .getString("kategori").toString());
                        map.put(KEY_SUB,
                                menuitemArray4.getJSONObject(i)
                                        .getString("subkategori").toString());
                        map.put(KEY_IDKAT, menuitemArray4.getJSONObject(i)
                                .getString("idkategori").toString());
                        map.put(KEY_IDSUB, menuitemArray4.getJSONObject(i)
                                .getString("idsubkategori").toString());
                        map.put(KEY_THUMB_URL, menuitemArray4.getJSONObject(i)
                                .getString("picture").toString());
                        map.put(KEY_HITS, menuitemArray4.getJSONObject(i)
                                .getString("hit").toString());
                        map.put(KEY_NOKONTRAKS, menuitemArray4.getJSONObject(i)
                                .getString("nokontrak").toString());
                        map.put(KEY_STAR, "4");
                        listRow.add(map);
                    }
                    /*
                     * list = (ListView) findViewById(R.id.lvRowsEnterpriseSearch);
                     * adapter = new SearchListAdapter(this, listRow);
                     * list.setAdapter(adapter); registerForContextMenu(list);
                     */

                    sisaList = sisaList - menuitemArray4.length();

                    xResult3star = MainActivity.getRequestSelect(dummyURL3star,
                            SearchActivity.this);
                    jObject3 = new JSONObject(xResult3star);
                    JSONArray menuitemArray3 = jObject3.getJSONArray("venterprise");

                    if (sisaList < menuitemArray3.length()) {
                        for (int i = 0; i < sisaList; i++) {
                            HashMap<String, String> map = new HashMap<String, String>();
                            map.put(KEY_IDENTERPRISE,
                                    menuitemArray3.getJSONObject(i)
                                            .getString("identerprise").toString());
                            map.put(KEY_NAMAENTERPRISE, menuitemArray3
                                    .getJSONObject(i).getString("nama").toString());
                            map.put(KEY_ALAMATENTERPRISE, menuitemArray3
                                    .getJSONObject(i).getString("address")
                                    .toString());
                            map.put(KEY_KATEGORI, menuitemArray3.getJSONObject(i)
                                    .getString("linkkat").toString());
                            map.put(KEY_LINK, menuitemArray3.getJSONObject(i)
                                    .getString("linksub").toString());
                            map.put(KEY_KAT, menuitemArray3.getJSONObject(i)
                                    .getString("kategori").toString());
                            map.put(KEY_SUB, menuitemArray3.getJSONObject(i)
                                    .getString("subkategori").toString());
                            map.put(KEY_IDKAT, menuitemArray3.getJSONObject(i)
                                    .getString("idkategori").toString());
                            map.put(KEY_IDSUB, menuitemArray3.getJSONObject(i)
                                    .getString("idsubkategori").toString());
                            map.put(KEY_THUMB_URL, menuitemArray3.getJSONObject(i)
                                    .getString("picture").toString());
                            map.put(KEY_HITS, menuitemArray3.getJSONObject(i)
                                    .getString("hit").toString());
                            map.put(KEY_NOKONTRAKS, menuitemArray3.getJSONObject(i)
                                    .getString("nokontrak").toString());
                            map.put(KEY_STAR, "3");
                            listRow.add(map);
                        }
                        /*
                         * list = (ListView)
                         * findViewById(R.id.lvRowsEnterpriseSearch); adapter = new
                         * SearchListAdapter(this, listRow);
                         * list.setAdapter(adapter); registerForContextMenu(list);
                         */
                    } else {

                        for (int i = 0; i < menuitemArray3.length(); i++) {
                            HashMap<String, String> map = new HashMap<String, String>();
                            map.put(KEY_IDENTERPRISE,
                                    menuitemArray3.getJSONObject(i)
                                            .getString("identerprise").toString());
                            map.put(KEY_NAMAENTERPRISE, menuitemArray3
                                    .getJSONObject(i).getString("nama").toString());
                            map.put(KEY_ALAMATENTERPRISE, menuitemArray3
                                    .getJSONObject(i).getString("address")
                                    .toString());
                            map.put(KEY_KATEGORI, menuitemArray3.getJSONObject(i)
                                    .getString("linkkat").toString());
                            map.put(KEY_LINK, menuitemArray3.getJSONObject(i)
                                    .getString("linksub").toString());
                            map.put(KEY_KAT, menuitemArray3.getJSONObject(i)
                                    .getString("kategori").toString());
                            map.put(KEY_SUB, menuitemArray3.getJSONObject(i)
                                    .getString("subkategori").toString());
                            map.put(KEY_IDKAT, menuitemArray3.getJSONObject(i)
                                    .getString("idkategori").toString());
                            map.put(KEY_IDSUB, menuitemArray3.getJSONObject(i)
                                    .getString("idsubkategori").toString());
                            map.put(KEY_THUMB_URL, menuitemArray3.getJSONObject(i)
                                    .getString("picture").toString());
                            map.put(KEY_HITS, menuitemArray3.getJSONObject(i)
                                    .getString("hit").toString());
                            map.put(KEY_NOKONTRAKS, menuitemArray3.getJSONObject(i)
                                    .getString("nokontrak").toString());
                            map.put(KEY_STAR, "3");
                            listRow.add(map);
                        }
                        /*
                         * list = (ListView)
                         * findViewById(R.id.lvRowsEnterpriseSearch); adapter = new
                         * SearchListAdapter(this, listRow);
                         * list.setAdapter(adapter); registerForContextMenu(list);
                         */

                        sisaList = sisaList - menuitemArray3.length();

                        xResult2star = MainActivity.getRequestSelect(dummyURL2star,
                                SearchActivity.this);
                        jObject2 = new JSONObject(xResult2star);
                        JSONArray menuitemArray2 = jObject2
                                .getJSONArray("venterprise");

                        if (sisaList < menuitemArray2.length()) {
                            for (int i = 0; i < sisaList; i++) {
                                HashMap<String, String> map = new HashMap<String, String>();
                                map.put(KEY_IDENTERPRISE, menuitemArray2
                                        .getJSONObject(i).getString("identerprise")
                                        .toString());
                                map.put(KEY_NAMAENTERPRISE, menuitemArray2
                                        .getJSONObject(i).getString("nama")
                                        .toString());
                                map.put(KEY_ALAMATENTERPRISE, menuitemArray2
                                        .getJSONObject(i).getString("address")
                                        .toString());
                                map.put(KEY_KATEGORI,
                                        menuitemArray2.getJSONObject(i)
                                                .getString("linkkat").toString());
                                map.put(KEY_LINK, menuitemArray2.getJSONObject(i)
                                        .getString("linksub").toString());
                                map.put(KEY_KAT, menuitemArray2.getJSONObject(i)
                                        .getString("kategori").toString());
                                map.put(KEY_SUB, menuitemArray2.getJSONObject(i)
                                        .getString("subkategori").toString());
                                map.put(KEY_IDKAT, menuitemArray2.getJSONObject(i)
                                        .getString("idkategori").toString());
                                map.put(KEY_IDSUB, menuitemArray2.getJSONObject(i)
                                        .getString("idsubkategori").toString());
                                map.put(KEY_THUMB_URL, menuitemArray2
                                        .getJSONObject(i).getString("picture")
                                        .toString());
                                map.put(KEY_HITS, menuitemArray2.getJSONObject(i)
                                        .getString("hit").toString());
                                map.put(KEY_NOKONTRAKS, menuitemArray2
                                        .getJSONObject(i).getString("nokontrak")
                                        .toString());
                                map.put(KEY_STAR, "2");
                                listRow.add(map);
                            }
                            /*
                             * list = (ListView)
                             * findViewById(R.id.lvRowsEnterpriseSearch); adapter =
                             * new SearchListAdapter(this, listRow);
                             * list.setAdapter(adapter);
                             * registerForContextMenu(list);
                             */
                        } else {
                            for (int i = 0; i < menuitemArray2.length(); i++) {
                                HashMap<String, String> map = new HashMap<String, String>();
                                map.put(KEY_IDENTERPRISE, menuitemArray2
                                        .getJSONObject(i).getString("identerprise")
                                        .toString());
                                map.put(KEY_NAMAENTERPRISE, menuitemArray2
                                        .getJSONObject(i).getString("nama")
                                        .toString());
                                map.put(KEY_ALAMATENTERPRISE, menuitemArray2
                                        .getJSONObject(i).getString("address")
                                        .toString());
                                map.put(KEY_KATEGORI,
                                        menuitemArray2.getJSONObject(i)
                                                .getString("linkkat").toString());
                                map.put(KEY_LINK, menuitemArray2.getJSONObject(i)
                                        .getString("linksub").toString());
                                map.put(KEY_KAT, menuitemArray2.getJSONObject(i)
                                        .getString("kategori").toString());
                                map.put(KEY_SUB, menuitemArray2.getJSONObject(i)
                                        .getString("subkategori").toString());
                                map.put(KEY_IDKAT, menuitemArray2.getJSONObject(i)
                                        .getString("idkategori").toString());
                                map.put(KEY_IDSUB, menuitemArray2.getJSONObject(i)
                                        .getString("idsubkategori").toString());
                                map.put(KEY_THUMB_URL, menuitemArray2
                                        .getJSONObject(i).getString("picture")
                                        .toString());
                                map.put(KEY_HITS, menuitemArray2.getJSONObject(i)
                                        .getString("hit").toString());
                                map.put(KEY_NOKONTRAKS, menuitemArray2
                                        .getJSONObject(i).getString("nokontrak")
                                        .toString());
                                map.put(KEY_STAR, "2");
                                listRow.add(map);
                            }
                            /*
                             * list = (ListView)
                             * findViewById(R.id.lvRowsEnterpriseSearch); adapter =
                             * new SearchListAdapter(this, listRow);
                             * list.setAdapter(adapter);
                             * registerForContextMenu(list);
                             */

                            sisaList = sisaList - menuitemArray2.length();

                            xResult1star = MainActivity.getRequestSelect(
                                    dummyURL1star, SearchActivity.this);
                            jObject1 = new JSONObject(xResult1star);
                            JSONArray menuitemArray1 = jObject1
                                    .getJSONArray("venterprise");

                            if (sisaList < menuitemArray1.length()) {
                                for (int i = 0; i < sisaList; i++) {
                                    HashMap<String, String> map = new HashMap<String, String>();
                                    map.put(KEY_IDENTERPRISE,
                                            menuitemArray1.getJSONObject(i)
                                                    .getString("identerprise")
                                                    .toString());
                                    map.put(KEY_NAMAENTERPRISE, menuitemArray1
                                            .getJSONObject(i).getString("nama")
                                            .toString());
                                    map.put(KEY_ALAMATENTERPRISE, menuitemArray1
                                            .getJSONObject(i).getString("address")
                                            .toString());
                                    map.put(KEY_KATEGORI, menuitemArray1
                                            .getJSONObject(i).getString("linkkat")
                                            .toString());
                                    map.put(KEY_LINK,
                                            menuitemArray1.getJSONObject(i)
                                                    .getString("linksub")
                                                    .toString());
                                    map.put(KEY_KAT, menuitemArray1
                                            .getJSONObject(i).getString("kategori")
                                            .toString());
                                    map.put(KEY_SUB,
                                            menuitemArray1.getJSONObject(i)
                                                    .getString("subkategori")
                                                    .toString());
                                    map.put(KEY_IDKAT,
                                            menuitemArray1.getJSONObject(i)
                                                    .getString("idkategori")
                                                    .toString());
                                    map.put(KEY_IDSUB,
                                            menuitemArray1.getJSONObject(i)
                                                    .getString("idsubkategori")
                                                    .toString());
                                    map.put(KEY_THUMB_URL, menuitemArray1
                                            .getJSONObject(i).getString("picture")
                                            .toString());
                                    map.put(KEY_HITS,
                                            menuitemArray1.getJSONObject(i)
                                                    .getString("hit").toString());
                                    map.put(KEY_NOKONTRAKS, menuitemArray1
                                            .getJSONObject(i)
                                            .getString("nokontrak").toString());
                                    map.put(KEY_STAR, "1");
                                    listRow.add(map);
                                }
                                /*
                                 * list = (ListView)
                                 * findViewById(R.id.lvRowsEnterpriseSearch);
                                 * adapter = new SearchListAdapter(this, listRow);
                                 * list.setAdapter(adapter);
                                 * registerForContextMenu(list);
                                 */
                            }
                        }
                    }
                }
            }

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

        for ( HashMap<String, String> item: listRow)
        {
            publishProgress(item);
        }

    return null;
    }

    // Update the progress
    @Override
    protected void onProgressUpdate(HashMap<String, String>... values) {


    }

    // after executing the code in the thread
    @Override
    protected void onPostExecute(Void result) {list = (ListView) findViewById(R.id.lvRowsEnterpriseSearch);
            adapter = new SearchListAdapter(SearchActivity.this, listRow);
            list.setAdapter(adapter);
            registerForContextMenu(list);
    }
}

在这种情况下,我必须将5个查询中的数据填充到1个listview中 当我运行应用程序时,布局会在1秒内出现,然后强制关闭。 当我对它进行debbug时,执行任务时会出现问题。

2 个答案:

答案 0 :(得分:1)

您的问题在于下面的代码(当您创建Aysnc任务时,doInBackground方法在UI线程的单独线程中运行。因此您无法从此方法更新UI。):

list = (ListView) findViewById(R.id.lvRowsEnterpriseSearch);
adapter = new SearchListAdapter(SearchActivity.this, listRow);
            list.setAdapter(adapter);
registerForContextMenu(list);

将上述代码移至onPostExecute方法。

答案 1 :(得分:0)

首先你创建asynctask就像(Void,HashMap(String,String),Void)意味着,

第一个参数:当你调用asynctask时传递给,听到是Void所以没有像new LoadViewTask().execute();那样传递

第二个参数:此参数从doInBackground方法返回,这是HashMap(String,String),因此您可以返回此参数。

所以你从listRow返回了doInBackground。 然后将代码doInBackground移至onPostExecute()

list = (ListView) findViewById(R.id.lvRowsEnterpriseSearch);
adapter = new SearchListAdapter(SearchActivity.this, listRow);
            list.setAdapter(adapter);
registerForContextMenu(list);

同样doInBackground方法在与UI线程分开的线程中运行。因此,您无法从此方法更新UI。