2016-09-24 13 views
2

Hier ist mein Spinner Klasse ScoutMetric<ArrayList<String>> erstreckt lösenDataSnapshot.getValue Typ konnte nicht T

public class ScoutMetric<T> { 
    private String mName; 
    private T mValue; 
    private int mType; 

    public ScoutMetric() { 
    } 

    public ScoutMetric(String name, T value) { 
     setName(name); 
     mValue = value; 
    } 

    public String getName() { 
     return mName; 
    } 

    public void setName(String name) { 
     mName = name; 
    } 

    public T getValue() { 
     return mValue; 
    } 

    public void setValue(T value) { 
     mValue = value; 
    } 

    public void setValue(DatabaseReference databaseReference, T value) { 
     databaseReference.child(Constants.FIREBASE_VALUE).setValue(value); 
     mValue = value; 
    } 

    public int getType() { 
     return mType; 
    } 

    public void setType(Integer type) { 
     mType = type; 
    } 

    public ScoutMetric<T> setType(int type) { 
     mType = type; 
     return this; 
    } 
} 

Ich versuche, den Wert wie folgt zu erhalten:

return snapshot.getValue(new GenericTypeIndicator<ScoutSpinner>() {}); 

Es sollte funktionieren, weil ich ausdrücklich ArrayList Art zu schaffen, bin: ScoutMetric<ArrayList<String>>

Was mache ich falsch? Oder ist das ein Fehler in Firebase? (BTW, mit v9.6.0)

PS: Das ist die Art von brutto disgustingness mit ich zu tun habe dieses Problem zu umgehen (nicht Spaß):

new ScoutSpinner(
    snapshot.child("name").getValue(String.class), 
    snapshot.child("value").getValue(new GenericTypeIndicator<ArrayList<String>>() {}), 
    snapshot.child("selectedValue").getValue(Integer.class) 
).setType(Constants.SPINNER); 

Hier ist meine Ausnahme in voller Länge:

09-23 21:20:35.913 19310-19310/? E/AndroidRuntime: FATAL EXCEPTION: main 
                Process: com.supercilex.robotscouter.debug, PID: 19310 
                java.lang.IllegalStateException: Could not resolve type T 
                 at com.google.android.gms.internal.zzamy$zza.zza(Unknown Source) 
                 at com.google.android.gms.internal.zzamy$zza.zze(Unknown Source) 
                 at com.google.android.gms.internal.zzamy$zza.zzce(Unknown Source) 
                 at com.google.android.gms.internal.zzamy.zze(Unknown Source) 
                 at com.google.android.gms.internal.zzamy.zzb(Unknown Source) 
                 at com.google.android.gms.internal.zzamy.zza(Unknown Source) 
                 at com.google.android.gms.internal.zzamy.zza(Unknown Source) 
                 at com.google.firebase.database.DataSnapshot.getValue(Unknown Source) 
                 at com.supercilex.robotscouter.scout.ScoutFragment$1.parseSnapshot(ScoutFragment.java:91) 
                 at com.supercilex.robotscouter.scout.ScoutFragment$1.parseSnapshot(ScoutFragment.java:67) 
                 at com.supercilex.robotscouter.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:155) 
                 at com.supercilex.robotscouter.scout.ScoutFragment$1.getItemViewType(ScoutFragment.java:139) 
                 at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5017) 
                 at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4970) 
                 at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029) 
                 at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414) 
                 at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377) 
                 at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578) 
                 at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3315) 
                 at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3124) 
                 at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1549) 
                 at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:305) 
                 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871) 
                 at android.view.Choreographer.doCallbacks(Choreographer.java:683) 
                 at android.view.Choreographer.doFrame(Choreographer.java:616) 
                 at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857) 
                 at android.os.Handler.handleCallback(Handler.java:751) 
                 at android.os.Handler.dispatchMessage(Handler.java:95) 
                 at android.os.Looper.loop(Looper.java:154) 
                 at android.app.ActivityThread.main(ActivityThread.java:6077) 
                 at java.lang.reflect.Method.invoke(Native Method) 
                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

Antwort

1

Ich hatte das gleiche Problem in meiner App. i gelöst mit Jackson in dieser Antwort:

Android Firebase Exception "Could not resolve type T"

So:

ObjectMapper mapper = new ObjectMapper(); 
ScoutSpinner spinner = mapper.convertValue(dataSnapshot.getValue(), ScoutSpinner.class); 
Verwandte Themen