gpt4 book ai didi

java - DataSnapshot.getValue 无法解析类型 T

转载 作者:行者123 更新时间:2023-11-30 10:38:29 26 4
gpt4 key购买 nike

这是我的微调类扩展 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);

这是我的异常(exception)情况:

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)

最佳答案

我在我的应用程序中遇到了同样的问题。我在这个答案中与 jackson 一起解决了:

Android Firebase Exception "Could not resolve type T"

像这样:

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

关于java - DataSnapshot.getValue 无法解析类型 T,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39672526/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com