【问题标题】:Android : why is my string variable not recognised outside my try - catch block?Android:为什么在我的 try-catch 块之外无法识别我的字符串变量?
【发布时间】:2017-06-14 22:00:49
【问题描述】:

当我使用下面的代码在 Logcat 中打印 getCategory 时,我得到 the category is Plumber(或任何 review.setCategory 设置的值。)

  //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 并没有解决它。任何帮助将不胜感激。

【问题讨论】:

    标签: android try-catch


    【解决方案1】:

    你需要指定getCategory的类型

    在下面的示例中,您可以访问在try 中设置的name

    String name = "";
    
    try{
    
        name = "asdasd";
    
    }catch (Exception e){
    
    }
    
    System.out.println(name);
    

    【讨论】:

    • 正在设置getCategory。问题是try 块可以在throw 之前设置getCategory
    • @RobertHarvey 然后我会在尝试捕获之前建议,而不是将其设置为空。设置默认类别
    • @Derek 你能解释一下默认类别是什么意思吗?
    • @CHarris 好吧,如果它只是一个字符串,我会将类别默认为“无”。然后它会显示,The category is none
    猜你喜欢
    • 2014-08-09
    • 1970-01-01
    • 2013-02-08
    • 2021-02-03
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 2015-10-27
    • 1970-01-01
    相关资源
    最近更新 更多