com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的值转换为 long
Posted
技术标签:
【中文标题】com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的值转换为 long【英文标题】:com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to long 【发布时间】:2019-12-09 05:08:12 【问题描述】:按照 firebase 数据库的文档尝试将数据检索到对象以供使用时,我遇到了错误。
这是我的对象模型
public class Cart
private String pid, pname, price, discount, quantity;
public Cart()
public Cart(String pid, String pname, String price, String discount, String quantity)
this.pid = pid;
this.pname = pname;
this.price = price;
this.discount = discount;
this.quantity = quantity;
public String getPid()
return pid;
public void setPid(String pid)
this.pid = pid;
public String getPname()
return pname;
public void setPname(String pname)
this.pname = pname;
public String getPrice()
return price;
public void setPrice(String price)
this.price = price;
public String getDiscount()
return discount;
public void setDiscount(String discount)
this.discount = discount;
public String getQuantity()
return quantity;
public void setQuantity(String quantity)
this.quantity = quantity;
最后,我得到错误的代码和行被注释掉了。
FirebaseRecyclerOptions<Cart> options =
new FirebaseRecyclerOptions.Builder<Cart>()
.setQuery(cartListRef.child("User View")
.child(Prevalent.currentOnlineUser.getPhn_num())
.child("Products"), Cart.class)
.build();
FirebaseRecyclerAdapter<Cart, CartViewHolder> adapter =
new FirebaseRecyclerAdapter<Cart, CartViewHolder>(options)
@Override
protected void onBindViewHolder(@NonNull CartViewHolder holder, int position, @NonNull Cart model)
holder.jProductName.setText(model.getPname());
holder.jProductPrice.setText("Price = Rs. " +model.getPrice());
holder.jProductQuantity.setText("Quantity = " +model.getQuantity());
@NonNull
@Override
public CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i)
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_items_layout, parent,false);
CartViewHolder holder = new CartViewHolder(view);
return holder;
;
recyclerView.setAdapter(adapter);
adapter.startListening();
这是我在 Firebase 中的数据库结构。
来自 logcat 的完整 Stacktrace 在这里。
E/androidRuntime: FATAL EXCEPTION: main
Process: com.dueaoinfo.shoppingapp, PID: 10511
com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to long
at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source:616)
at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source:301)
at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source:0)
at com.google.android.gms.internal.firebase_database.zzku.zza(Unknown Source:71)
at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source:146)
at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source:0)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:29)
at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:15)
at com.firebase.ui.common.BaseCachingSnapshotParser.parseSnapshot(BaseCachingSnapshotParser.java:35)
at com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:52)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:106)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:122)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1888)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:407)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1092)
at android.view.Choreographer.doCallbacks(Choreographer.java:888)
at android.view.Choreographer.doFrame(Choreographer.java:816)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1078)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
【问题讨论】:
请添加产生此错误的代码。 我刚刚添加了一个代码。请帮帮我 欢迎来到 ***。请在下面查看我的答案。 【参考方案1】:quantity
和 discount
是 Firebase 数据库中的 Long
值,在 Cart
类中它被声明为 String
。这是抛出错误。像下面这样声明Cart
类。
public class Cart
private String pid, pname, price;
private Long discount, quantity;
public Cart()
public Cart(String pid, String pname, String price, Long discount, Long quantity)
this.pid = pid;
this.pname = pname;
this.price = price;
this.discount = discount;
this.quantity = quantity;
public String getPid()
return pid;
public void setPid(String pid)
this.pid = pid;
public String getPname()
return pname;
public void setPname(String pname)
this.pname = pname;
public String getPrice()
return price;
public void setPrice(String price)
this.price = price;
public Long getDiscount()
return discount;
public void setDiscount(Long discount)
this.discount = discount;
public Long getQuantity()
return quantity;
public void setQuantity(Long quantity)
this.quantity = quantity;
希望能解决您的问题。
【讨论】:
感谢您的帮助,但我遇到了同样的问题 嘿,我认为discount
也是Long
值。我已经更新了我的答案。您能否检查更新的答案并让我知道这是否有效?谢谢。
非常感谢您的帮助。它完美地工作。还有一个疑问如何找出它是字符串还是 Long 值???再次感谢....
很高兴知道这有帮助!不客气。带引号的值为String
,否则为Long
或Integer
。希望有帮助!以上是关于com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的值转换为 long的主要内容,如果未能解决你的问题,请参考以下文章