【问题标题】:How properly set fields in anonymous class and use them in main class?如何正确设置匿名类中的字段并在主类中使用它们?
【发布时间】:2019-05-30 14:15:00
【问题描述】:

我正在 Android Studio 中制作应用程序,但无法在匿名类中设置字段值。

我知道“receivedNumber”总是不同于“-1”并且值“isExist”必须为“true”,但在日志中为“false”。 int 字段也会发生同样的事情——总是返回“0”。 我应该怎么做才能在匿名类中正确设置字段并在我的主类中使用它们。

private void isNumberExist()
{
        String URL = "some url...";
        final boolean[] isExist = new boolean[1];
        objectRequest = new JsonObjectRequest(
                Request.Method.GET,
                URL,
                null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            int receivedNumber = response.getInt("id");
                            if (receivedNumber == -1) {                           
                                isExist[0] = false;                          
                            } else {     
                                isExist[0] = true;              
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                       //error
                    }
                }
        );
        Log.e("isExist","value"+ isExist[0]);
        requestQueue.add(objectRequest);
        requestQueue.cancelAll(this);
}

【问题讨论】:

  • Log.e("isExist","value"+ isExist[0]); 可能在调用onResponse 之前被执行。
  • @Justin 我把 Log.e() 放在方法的末尾,结果是一样的。

标签: java android-studio android-volley field anonymous-class


【解决方案1】:

它将始终为 false,因为您在执行 http(s) 请求之前正在记录 isExist[0]

请试试这个

private void isNumberExist()
{
    String URL = "some url...";
    final boolean[] isExist = new boolean[1];
    objectRequest = new JsonObjectRequest(
            Request.Method.GET,
            URL,
            null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        int receivedNumber = response.getInt("id");
                        if (receivedNumber == -1) {                           
                            isExist[0] = false;                          
                        } else {     
                            isExist[0] = true;              
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    // New log location
                    Log.e("isExist","value"+ isExist[0]);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //error
                }
            }
            );
    requestQueue.add(objectRequest);
    requestQueue.cancelAll(this);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多