这是我的微调器类,扩展了ScoutMetric<ArrayList<String>>
:
public class ScoutSpinner extends ScoutMetric<ArrayList<String>> {
private int mSelectedValue;
public ScoutSpinner() {
}
public ScoutSpinner(String name, ArrayList<String> values, int selectedValue) {
super(name, values);
mSelectedValue = selectedValue;
}
public int getSelectedValue() {
return mSelectedValue;
}
public void setSelectedValue(int selectedValue) {
mSelectedValue = selectedValue;
}
public void setSelectedValue(DatabaseReference databaseReference, int selectedValue) {
databaseReference.child("selectedValue").setValue(selectedValue);
mSelectedValue = selectedValue;
}
}
这是我的ScoutMetric类:
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;
}
}
我正在尝试获得这样的价值:
return snapshot.getValue(new GenericTypeIndicator<ScoutSpinner>() {});
它应该工作,因为我明确创建了
ArrayList
类型:ScoutMetric<ArrayList<String>>
我究竟做错了什么?还是这是Firebase中的错误? (顺便说一句,使用v9.6.0)
PS:这是我正在处理的那种令人厌恶的事情,目的是为了规避这个问题(不好玩):
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);
这是我的全部例外情况:
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)
最佳答案
我的应用程式有相同的问题。我在这个答案中与杰克逊解决了:
Android Firebase Exception "Could not resolve type T"
像这样:
ObjectMapper mapper = new ObjectMapper();
ScoutSpinner spinner = mapper.convertValue(dataSnapshot.getValue(), ScoutSpinner.class);