【问题标题】:How to fix " java.lang.NoSuchMethodError: com.google.firebase.database.DatabaseReference.setValue"如何修复“java.lang.NoSuchMethodError:com.google.firebase.database.DatabaseReference.setValue”
【发布时间】:2019-05-28 22:45:33
【问题描述】:

我正在我的应用程序中设置当前位置。我需要在哪里设置编码?

    @Override
public void onLocationChanged(Location location) {
    mLastLocation = location;
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(11));

    //Geo Fire for current location
    String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference refAvailable = FirebaseDatabase.getInstance().getReference("driversAvailable");
    DatabaseReference refWorking = FirebaseDatabase.getInstance().getReference("driversWorking");

    GeoFire geoFireAvailable = new GeoFire(refAvailable);
    GeoFire geoFireWorking = new GeoFire(refWorking);

    switch (customerId){
        case "":

            geoFireWorking.removeLocation(userId);
            geoFireAvailable.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
                @Override
                public void onComplete(String key, DatabaseError error) {

                }
            });

            break;
            default:
                geoFireAvailable.removeLocation(userId);
                geoFireWorking.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
                    @Override
                    public void onComplete(String key, DatabaseError error) {

                    }
                });

                break;
    }


}

以下是我得到的错误:

java.lang.NoSuchMethodError: com.google.firebase.database.DatabaseReference.setValue 在 com.firebase.geofire.GeoFire.removeLocation(GeoFire.java:215) 在 com.firebase.geofire.GeoFire.removeLocation(GeoFire.java:192) 在 com.example.webforest.quickaidlikeuber2nd.DriverMapActivity.onLocationChanged(DriverMapActivity.java:184)

【问题讨论】:

  • 你可以尝试使用geoFire.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener(){ }); 代替
  • 成功了!!!!!!非常感谢
  • 很高兴我能帮上忙...我会将其作为答案发布,以便将其从未回答的问题列表中删除。

标签: android dictionary firebase-realtime-database geofire currentlocation


【解决方案1】:

Geofire 发生了一些变化,现在要使 removeLocation 工作,您必须像这样添加一个侦听器:

 //after updating 
                    geoFireWorking.removeLocation("id of the node you want to remove", new GeoFire.CompletionListener() {
                        @Override
                        public void onComplete(String key, DatabaseError error) {

                        }
                    });

【讨论】:

    【解决方案2】:

    当您使用setLocation() 方法而不使用GeoFire.CompletionListener() 时,此错误似乎会自动显现。如果您实施CompletionListener,这应该可以解决问题。

    因此,替换

    geoFire.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()));
    

    与

    geoFire.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener(){ });
    

    编辑 2:
    如果您仍然遇到困难,您可能需要适应不同的方法。

    替换以下代码:

    geoFireAvailable.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
        @Override
        public void onComplete(String key, DatabaseError error) {
    
        }
    });
    

    使用以下代码:

    GeoHash geoHash = new GeoHash(new GeoLocation(location.getLatitude(),location.getLongitude()));
    Map<String, Object> updates = new HashMap<>();
    updates.put("g", geoHash.getGeoHashString());
    updates.put("l", Arrays.asList(location.getLatitude(),location.getLongitude()));
    geoFireAvailable.setValue(updates,geoHash.getGeoHashString());
    

    【讨论】:

    • 当我在 \n switch (customerId){ case "": geoFireWorking.removeLocation(userId); geoFireAvailable.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() { @Override public void onComplete(String key, DatabaseError error) { } });
    • @Raifur-rahim 使用新代码、问题和您要执行的操作来编辑问题。我会审查它
    • @Raifur-rahim 再次检查答案
    • 先生,我是新来的,如果您能具体描述一下,那将会很有帮助
    • @Raifur-rahim 大声笑,不要称我为“先生”......我知道这是尊重,但我还没有达到那个水平。我已经编辑了答案,再看看
    猜你喜欢
    • 1970-01-01
    • 2019-01-12
    • 2020-01-13
    • 2019-08-20
    • 2019-02-20
    • 2017-10-24
    • 2014-09-07
    • 2021-10-19
    • 2017-05-09
    相关资源
    最近更新 更多