java.lang.RuntimeException:执行doInBackground()时发生错误

时间:2014-06-05 21:38:42

标签: java android

我已经看过和其他相关的帖子,我明白错误是在doInBackground方法中理解和发生的错误,但我无法理解我必须改变或传达onPostExecute方法的代码位置。如果有人我知道我应该改变什么代码,谢谢。以下是doInBackground方法等的代码。

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

        // Check your log cat for JSON response
        Log.d("All products:",json.toString()); //141 line

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

            if (success == 1) {
                // products found
                // Getting Array of Products
                products = json.getJSONArray(TAG_CLASSES);

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

                    // Storing each json item in variable
                    String id = c.getString(TAG_PID);
                    String name = c.getString(TAG_NAME);

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

                    // adding each child node to HashMap key => value
                    map.put(TAG_PID, id);
                    map.put(TAG_NAME, name);

                    // adding HashList to ArrayList
                    productsList.add(map);
                }
            } else {
                // no products found
                // Launch Add New product Activity
                Intent i = new Intent(getApplicationContext(),NewProductActivity.class);
                // Closing all previous activities
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

     return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        AllProductsActivity.this, productsList,
                        R.layout.list_item, new String[] { TAG_PID,
                                TAG_NAME},
                        new int[] { R.id.id, R.id.name });
                // updating listview
                setListAdapter(adapter);
            }
        });

    }

}

以下是错误

06-05 20:06:49.454: E/Buffer Error(1981): Error converting result       java.lang.NullPointerException: lock == null
06-05 20:06:49.509: E/JSON Parser(1981): Error parsing data org.json.JSONException: End of input at character 0 of 
06-05 20:06:49.584: W/dalvikvm(1981): threadid=10: thread exiting with uncaught exception (group=0xb4da3908)
06-05 20:06:49.664: E/AndroidRuntime(1981): FATAL EXCEPTION: AsyncTask #1
06-05 20:06:49.664: E/AndroidRuntime(1981): java.lang.RuntimeException: An error occurred while executing doInBackground()
06-05 20:06:49.664: E/AndroidRuntime(1981):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-05 20:06:49.664: E/AndroidRuntime(1981):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
06-05 20:06:49.664: E/AndroidRuntime(1981):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
06-05 20:06:49.664: E/AndroidRuntime(1981):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
06-05 20:06:49.664: E/AndroidRuntime(1981):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-05 20:06:49.664: E/AndroidRuntime(1981):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-05 20:06:49.664: E/AndroidRuntime(1981):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-05 20:06:49.664: E/AndroidRuntime(1981):     at jav a.lang.Thread.run(Thread.java:856)
06-05 20:06:49.664: E/AndroidRuntime(1981): Caused by: java.lang.NullPointerException
06-05 20:06:49.664: E/AndroidRuntime(1981):     at com.panos.appphpconnect.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:141)
06-05 20:06:49.664: E/AndroidRuntime(1981):     at com.panos.appphpconnect.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:1)
06-05 20:06:49.664: E/AndroidRuntime(1981):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-05 20:06:49.664: E/AndroidRuntime(1981):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-05 20:06:49.664: E/AndroidRuntime(1981):     ... 4 more
06-05 20:07:03.554: W/Trace(1981): Unexpected value from nativeGetEnabledTags: 0

1 个答案:

答案 0 :(得分:1)

第141行中的Objet json为空。您有一个解析器错误,这意味着解析器无法读取您的对象,在这种情况下,它将返回null。

下一行,您尝试使用json对象(json.toString())并获得NullPointerException

查看您尝试在线解析的对象:

JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

该行返回一个空对象。