致命异常:AsyncTask#1

时间:2012-07-11 21:07:53

标签: exception task

我有一个致命异常:AsyncTask#1在下面的部分,但不知道为什么。

    /**
     * getting Charts from URL
     * */
    protected String doInBackground(String... args) {
        // Building Parameters
        ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_read, "POST", params);

        // Check your log cat for JSON response
        Log.d("Charts: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products

                JSONArray charts = json.getJSONArray(TAG_CHARTS);

                // looping through All Products
                for(int i=0;i<charts.length();i++) {
                    JSONObject json_data = charts.getJSONObject(i);

                    String id = json_data.getString("id");
                    String interpret = json_data.getString("interpret");
                    String titel = json_data.getString("titel");
                    String album = json_data.getString("album");
                    String albumcover = json_data.getString("albumcover");
                    String likes = json_data.getString("likes");

                    HashMap<String, String> map = new HashMap<String, String>();

                    map.put(TAG_ID, id);
                    map.put(TAG_INTERPRET, interpret);
                    map.put(TAG_TITEL, titel);
                    map.put(TAG_ALBUM, album);
                    map.put(TAG_ALBUMCOVER, albumcover);
                    map.put(TAG_LIKES, likes);       

                    songslist.add(map);

                }
            } else {
                // no products found
                // Launch Add New product Activity
                Intent i = new Intent(getApplicationContext(),
                        CreateSong.class);
                // Closing all previous activities
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            } 
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

这是我的第一个问题。希望你知道该怎么做。我从数据库中获取数据并将它们放入JSON数组然后放入Hashmap,但似乎错误来自JSON。

编辑:登录CAT

07-11 23:20:48.320: D/CLIPBOARD(13241): Hide Clipboard dialog at Starting input: finished by someone else... !
07-11 23:20:52.510: D/dalvikvm(13241): GC_FOR_ALLOC freed 139K, 3% free 9004K/9223K, paused 13ms
07-11 23:20:52.650: E/JSON Parser(13241): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
07-11 23:20:52.650: W/dalvikvm(13241): threadid=11: thread exiting with uncaught exception (group=0x40c331f8)
07-11 23:20:52.675: E/AndroidRuntime(13241): FATAL EXCEPTION: AsyncTask #1
07-11 23:20:52.675: E/AndroidRuntime(13241): java.lang.RuntimeException: An error occured while executing doInBackground()
07-11 23:20:52.675: E/AndroidRuntime(13241):    at android.os.AsyncTask$3.done(AsyncTask.java:278)
07-11 23:20:52.675: E/AndroidRuntime(13241):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-11 23:20:52.675: E/AndroidRuntime(13241):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-11 23:20:52.675: E/AndroidRuntime(13241):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-11 23:20:52.675: E/AndroidRuntime(13241):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-11 23:20:52.675: E/AndroidRuntime(13241):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-11 23:20:52.675: E/AndroidRuntime(13241):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-11 23:20:52.675: E/AndroidRuntime(13241):    at java.lang.Thread.run(Thread.java:856)
07-11 23:20:52.675: E/AndroidRuntime(13241): Caused by: java.lang.NullPointerException
07-11 23:20:52.675: E/AndroidRuntime(13241):    at de.andreasgloeckner.ShowCharts$LoadAllProducts.doInBackground(ShowCharts.java:172)
07-11 23:20:52.675: E/AndroidRuntime(13241):    at     de.andreasgloeckner.ShowCharts$LoadAllProducts.doInBackground(ShowCharts.java:1)
07-11 23:20:52.675: E/AndroidRuntime(13241):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
07-11 23:20:52.675: E/AndroidRuntime(13241):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-11 23:20:52.675: E/AndroidRuntime(13241):    ... 4 more

2 个答案:

答案 0 :(得分:0)

您的代码中有一个NullPointerException(ShowCharts.java,第172行)。现在弄清楚是哪一行。您正在访问一个空对象,或者只是在这里共享第172行。它可能是songslist个对象。我没有在你共享的代码块中看到初始化。

Caused by: java.lang.NullPointerException
    07-11 23:20:52.675: E/AndroidRuntime(13241):    at de.andreasgloeckner.ShowCharts$LoadAllProducts.doInBackground(ShowCharts.java:172)

答案 1 :(得分:0)

从doInBackground方法内部,您无法更改或触摸任何UI元素,因此您无法调用另一个活动,您需要做的是从Asynctask中调用onPostExecute方法中的另一个活动