安卓(未处理的异常:org.json.JSONException)异常、未处理、org、json

2023-09-04 06:49:55 作者:旧年素颜、君记否

家伙。不能输出JSON字符串/对象到TextView中。 开始在Android的Studio编程数天前,所以...需要你的帮助,伙计们。 我有mainActivity,AllProductsActivity和EditProductActivity。所有产品的活动越来越JSON数组从mysql数据库,并显示在ListView控件。点击列表项后它去EditProductActivity.That工作正常。但我不明白什么,我需要做的与此错误...

guys. Can't output JSON string/object into TextView. Started programming in Android Studio few day ago, so...need your help, guys. I have mainActivity, AllProductsActivity and EditProductActivity. All product activity getting JSON array from mysql database and showing in ListView. After clicking on List Item it goes to EditProductActivity.That works fine. But i can't understand what i need to do with this error...

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

Unhandled exception: org.json.JSONException

推荐答案

添加一个try / catch

Add a try/catch

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
        }