-1
當我打印getCategory
在logcat中與下面的代碼我得到the category is Plumber
(或任何review.setCategory
設爲。)Android:爲什麼我的字符串變量在我的try-catch塊之外無法識別?
//post the review that has been clicked in the ListView and send it to
// ContactView.php and from that get other review details
StringRequest stringRequest = new StringRequest(Request.Method.POST, ContactView_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//hide the 'loading' box when the page loads
pDialog.dismiss();
//toast the response of ContactView.php, which has been converted to a
//JSON array in the Php file with JSON encode
Toast.makeText(ContactView.this, response, Toast.LENGTH_LONG).show();
for (int i = 0; i < response.length(); i++) {
getCategory = null;
try {
JSONArray responseObject = new JSONArray(response);
JSONObject obj = responseObject.getJSONObject(i);
Review review = new Review();
review.setCategory(obj.getString("category"));
review.setName(obj.getString("name"));
review.setPhone(obj.getString("phone"));
review.setAddress(obj.getString("address"));
review.setComment(obj.getString("comment"));
//we are getting the review id so we can pull extra needed info, like Address etc
//review.setReviewid(obj.getString("reviewid"));
Toast.makeText(ContactView.this, review.getCategory(), Toast.LENGTH_SHORT).show();
//reviewList.add(review);
getCategory = review.getCategory();
System.out.println("the category is " + getCategory);
} catch (JSONException e) {
Log.e("MYAPP", "unexpected JSON exception", e);
// Do something to recover ... or kill the app.
}
}
但是當我把System.out.println("the category is " + getCategory);
其他地方的活動中,try-catch
外面,我得到the category is null
我在Stackoverflow上讀了很多帖子,通過在try-catch
塊之前聲明getCategory = null;
來解決這個問題,但t並沒有解決這個問題。任何幫助,將不勝感激。
他*的*'設置的getCategory'裏面。問題在於'try'塊可以在'getCategory'設置之前拋出''。 –
@RobertHarvey然後我會建議嘗試捕獲之前,而不是將其設置爲null。設置默認類別 – Tony
@Derek你能解釋一下默認類別的含義嗎? – CHarris