【问题标题】:Java anonymous class (implementing an interface) callback is null when passed to another functionJava匿名类(实现接口)回调在传递给另一个函数时为空
【发布时间】:2018-08-02 01:56:58
【问题描述】:

我正在尝试为 Android Java JsonObjectRequests 实现回调。我发现几十个帖子显示的代码几乎是逐字记录的。我的问题是 callback 为空。查看两个内联 cmets。

public interface JsonObjectCallbackInterface {
    void onSuccess(JSONObject result);
}
class DispatchBuddyBase {
    //...

    public void addGmapMarker(String inAddress) {
        final String address = DBB.prepareAddress(inAddress);
        DatabaseReference ref = DBB.getTopPathRef("/geocodedAddresses");

        ref.orderByChild("address")
                .equalTo(address)
                .addListenerForSingleValueEvent(
                new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Log.i(TAG, address+" stored: "+dataSnapshot.exists());
                if (!dataSnapshot.exists()) {
                    DBB.getLatLng(
                           "17 River Rd Meriden CT 06451",
                            new JsonObjectCallbackInterface() {
                         //      ^--- why is this passing a **null?**

                        @Override
                        public void onSuccess(JSONObject response) {
                            Log.i(TAG, "got a json body:"+response.toString());
                            addGmapMarker(response);
                        }
                    });
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }
}
public class DispatchesActivity extends AppCompatActivity implements
    OnMapReadyCallback,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    ResultCallback<LocationSettingsResult> {
    //...

    public void getLatLng(final String address,
                          final JsonObjectCallbackInterface callback) {
        // why does this get a **null** callback? -------------^

        Log.e(TAG, "callback is: "+callback);
        String url = "https://maps.googleapis.com/maps/api/geocode/json?address="
                + address;

        JsonObjectRequest jsObjRequest = new JsonObjectRequest
                (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        Log.e(TAG,"Response: " + response.toString());
                        DatabaseReference ref = getTopPathRef("/geocodedAddresses").push();

                        Map<String, Object> u = new HashMap<>();
                        u.put("address", address);
                        u.put("geocoded", response.toString());

                        ref.updateChildren(u, new DatabaseReference.CompletionListener() {
                            @Override
                            public void onComplete(DatabaseError databaseError,
                                                   DatabaseReference databaseReference) {
                                if (databaseError != null) {
                                    Log.e(TAG,"Geocoded address could not be saved "
                                            + databaseError.getMessage());
                                } else {
                                    Log.e(TAG,"Geocoded address saved successfully.");
                                }
                            }
                        });
                        Log.e(TAG, "callback2 is: "+callback);
                        callback.onSuccess(response);
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub
                    }
                });

        DBVolley.getInstance(context)
                .addToRequestQueue(jsObjRequest);
    }
}

需要明确的是,我曾经注意到callback 在我测试时不是空的只有一次。从那时起,重复运行没有产生非空回调。我才 8 天进入 Java,所以如果我做了一些幼稚的事情,请原谅。

DispatchBuddyBase 单例在主活动中仅被实例化一次,DBVolley 也是如此。所有其他参考都通过DispatchBuddyBase.getInstance() 获得相同的信息。我在其他地方的匿名类没有任何问题。

Volley 代码很简单,可以很好地触发请求,JsonObjectRequest 成功地从 Google 获得了我期望的一切。除了这个回调之外,其他一切都运行良好。

我需要解决什么问题才能正确传递此回调?

【问题讨论】:

  • new Whatever()从不为空:你的问题一定在别处
  • 啊,如果你不能解决你的问题,把它贴在堆栈上*,你会很快弄清楚并把头撞在墙上......我会标记我的帖子很快解决了
  • 标记删除,因为整个事情都是一个误解(请参阅下面的 OP 答案)

标签: java interface callback jsonobjectrequest geocoder


【解决方案1】:

好的,标记为已解决。奥卡姆剃刀。我无意中过多地按了 ctrl-z,并且在另一段代码中取消了注释,该代码段使用回调的文字 null 参数进行调用。

以上代码完美运行。

【讨论】:

    猜你喜欢
    • 2013-10-26
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 2023-03-06
    • 2015-02-14
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多