Java classe anonima (implementando un'interfaccia) callback è nullo quando passata ad un'altra funzione

voti
1

Sto tentando di implementare una funzione di callback per Android Java JsonObjectRequests. Ho trovato decine di messaggi che mostrano il codice che è quasi parola per parola. Il mio problema è che il callbackè nullo. Vedere i due commenti in linea.

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);
    }
}

Per essere chiari, una volta ho fatto notare che callbacknon era nulla solo una volta , quando stavo testando. Ripetere piste non hanno prodotto una richiamata non nullo da allora. Sono solo 8 giorni in Java, quindi per favore perdoni se ho fatto qualcosa di ingenuo.

Il DispatchBuddyBasesingleton è istanziato nell'attività principale solo una volta, e DBVolleyallo stesso modo. Tutti i riferimenti addizionali ottenere la stessa via DispatchBuddyBase.getInstance(). Non ho avuto alcun problema con le classi anonime altrove.

Il codice Volley è ossa nude e spara le richieste più che bene, il JsonObjectRequest ottiene con successo tutto quello che mi aspetto da Google. Tutto il resto è gonfiano lavorando ad eccezione di questo callback.

Di cosa ho bisogno per riparare a passare correttamente questo callback?

È pubblicato 22/02/2018 alle 09:22
fonte dall'utente
In altre lingue...                            


1 risposte

voti
0

Ok, marcatura questo risolto. Rasoio di Occam. Mi ha colpito involontariamente Ctrl-Z troppo e commentate in un'altra sezione di codice che stava chiamando con un parametro zero letterale per la richiamata.

Il codice di cui sopra funziona perfettamente.

Risposto il 22/02/2018 a 21:29
fonte dall'utente

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more