1

彼ら。JSON 文字列/オブジェクトを TextView に出力できません。数日前に Android Studio でプログラミングを始めたので、皆さんの助けが必要です。mainActivity、AllProductsActivity、EditProductActivity があります。mysql データベースから JSON 配列を取得し、ListView に表示するすべての製品アクティビティ。List Item をクリックすると、EditProductActivity に移動します。しかし、私はこのエラーで何をする必要があるのか​​ 理解できません...

class GetProductDetails extends AsyncTask<String, String, JSONObject> {


    //Show progress dialog

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(EditProductActivity.this);
        pDialog.setMessage("Loading product details. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }


    //Getting detail info in background


    protected JSONObject doInBackground(String... args) {

        JSONObject product = null;
        // check status success tag
        int success;

        try {
            // parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("pid", pid));

            // getting product from HTTP
            JSONObject json = jsonParser.makeHttpRequest(url_product_details, "GET", params);

            Log.d("Single Product Details", json.toString());

            success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
                // Getting detail info successful
                JSONArray productObj = json.getJSONArray(TAG_PRODUCT);

                // first object from JSON Array
                product = productObj.getJSONObject(0);

            } else {
                // product with that pid not found
                Toast.makeText(getApplicationContext(), "pid not found", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getApplicationContext(), AllProductsActivity.class);
                startActivity(intent);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return product;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * *
     */
    protected void onPostExecute(JSONObject product) {
        if (product != null) {
            setContentView(R.layout.activity_edit_product);

            // product with this pid found
            // Text View
            txtName = (TextView) findViewById(R.id.showName);
            txtPrice = (TextView) findViewById(R.id.showPrice);
            txtDesc = (TextView) findViewById(R.id.showDescription);

            // display product data in EditText

           txtName.setText(product.getString(TAG_NAME));
          txtPrice.setText(product.getString(TAG_PRICE));
           txtDesc.setText(product.getString(TAG_DESCRIPTION));

        }
        // close progress dialog
        pDialog.dismiss();
    }
}

}

エラー:

txtName.setText(product.getString(TAG_NAME));
txtPrice.setText(product.getString(TAG_PRICE));
txtDesc.setText(product.getString(TAG_DESCRIPTION));

未処理の例外: org.json.JSONException

4

1 に答える 1

4

トライ/キャッチを追加

try {
            txtName.setText(product.getString(TAG_NAME));
            txtPrice.setText(product.getString(TAG_PRICE));
            txtDesc.setText(product.getString(TAG_DESCRIPTION));
        } catch (JSONException e) {
            // Do something with the exception
        }
于 2015-05-17T16:27:47.827 に答える