0

我想将数据库中的卡路里值存储在一个名为 x 的数组中,为此我必须在从数据库中重新训练数据的循环中实现它;但是,当我这样做时,程序会崩溃,从而导致错误。谁能告诉我问题出在哪里以及如何解决?!

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 reponse
            Log.d("All Products: ", json.toString());

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

                if (success == 1) {
                    // products found
                    // Getting Array of Products
                    Log.d("All Products: ", json.toString());
                    products = json.getJSONArray(TAG_PRODUCT);



                    // 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); // also name
                        String category = c.getString(TAG_CATEGORY );
                        Double calory = c.getDouble(TAG_CALORY);

                        Log.d("After X Declaration ", calory.toString());

                        // NO NEED FOR THEM !!
                        /*
                        String unit = c.getString(TAG_UNIT );
                        String category = c.getString(TAG_CATEGORY );
                        String calory = c.getString(TAG_CALORY );
                        String carbohydrate = c.getString(TAG_CARBOHYDRATE ); */


                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();
                        x[i]=calory;
                       // Log.d("X items ", x.toString());
                        // adding each child node to HashMap key => value
                        map.put(TAG_PID, id);
                        map.put(TAG_CATEGORY, category);
                        //map.put(TAG_NAME, name);

                        // adding HashList to ArrayList
                        productsList.add(map);
                        Log.d("After X Declaration ", json.toString());
                    }
                } 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;
        }
4

0 回答 0