com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为 com.example.gatewayrest
Posted
技术标签:
【中文标题】com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为 com.example.gatewayrestaurant.model1 类型【英文标题】:com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.gatewayrestaurant.model1 【发布时间】:2021-05-02 00:20:08 【问题描述】:在片段中显示来自 Firebase 的数据 收到此错误 我不知道错误是什么 任何帮助将不胜感激 制作一个简单的应用程序,其中 Firebase 数据显示在应用程序中。 ** 我已经尝试过正常的活动。 但在片段中,它不起作用**
Model1 类
package com.example.gatewayrestaurant;
public class model1
String image,name,price;
public model1()
public model1(String image, String name, String price)
this.image = image;
this.name = name;
this.price = price;
public String getImage()
return image;
public void setImage(String image)
this.image = image;
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getPrice()
return price;
public void setPrice(String price)
this.price = price;
我的适配器
package com.example.gatewayrestaurant;
public class myadaptersoutindian extends FirebaseRecyclerAdapter<model1,myadaptersoutindian.myviewholder>
public myadaptersoutindian(@NonNull FirebaseRecyclerOptions<model1> options)
super(options);
@Override
protected void onBindViewHolder(@NonNull myviewholder holder, int position, @NonNull model1 model)
Glide.with(holder.img.getContext()).load(model.getImage()).into(holder.img);
holder.menuTitle.setText(model.getName());
holder.price.setText(model.getPrice());
@NonNull
@Override
public myviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.southindiancardview,parent,false);
return new myviewholder(view);
public class myviewholder extends RecyclerView.ViewHolder
ImageView img;
TextView menuTitle,price;
public myviewholder(@NonNull View itemView)
super(itemView);
img=itemView.findViewById(R.id.menu_image);
menuTitle=itemView.findViewById(R.id.menu_title);
price=itemView.findViewById(R.id.price);
片段
public class ftab1 extends Fragment
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
RecyclerView recview;
myadaptersoutindian adapter;
public ftab1()
public static ftab1 newInstance(String param1, String param2)
ftab1 fragment = new ftab1();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
if (getArguments() != null)
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View view=inflater.inflate(R.layout.fragment_ftab1, container, false);
recview=(RecyclerView)view.findViewById(R.id.recview);
recview.setLayoutManager(new LinearLayoutManager(getContext()));
FirebaseRecyclerOptions<model1> options =
new FirebaseRecyclerOptions.Builder<model1>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("southindian"), model1.class)
.build();
adapter=new myadaptersoutindian(options);
recview.setAdapter(adapter);
return view;
@Override
public void onStart()
super.onStart();
adapter.startListening();
@Override
public void onStop()
super.onStop();
adapter.stopListening();
错误
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.gatewayrestaurant.model1
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:436)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
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:36)
at com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:52)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:109)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:149)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:23750)
at android.view.ViewGroup.layout(ViewGroup.java:7277)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:23750)
at android.view.ViewGroup.layout(ViewGroup.java:7277)
at androidx.viewpager.widget.ViewPager.onLayout(ViewPager.java:1775)
at android.view.View.layout(View.java:23750)
at android.view.ViewGroup.layout(ViewGroup.java:7277)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1855)
at android.view.View.layout(View.java:23750)
at android.view.ViewGroup.layout(ViewGroup.java:7277)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:23750)
at android.view.ViewGroup.layout(ViewGroup.java:7277)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:530)
at android.view.View.layout(View.java:23750)
at android.view.ViewGroup.layout(ViewGroup.java:7277)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:23750)
at android.view.ViewGroup.layout(ViewGroup.java:7277)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
2021-01-28 10:42:28.545 3914-3914/com.example.gatewayrestaurant E/AndroidRuntime: at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:23750)
at android.view.ViewGroup.layout(ViewGroup.java:7277)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:1099)
at android.view.View.layout(View.java:23750)
at android.view.ViewGroup.layout(ViewGroup.java:7277)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3712)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3164)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2225)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9126)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:999)
at android.view.Choreographer.doCallbacks(Choreographer.java:797)
at android.view.Choreographer.doFrame(Choreographer.java:732)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:984)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8167)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
任何其他在片段中显示 firebase 数据的方法也可以使用。
【问题讨论】:
【参考方案1】:您收到以下错误:
com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为 com.example.gatewayrestaurant.model1 类型
因为您传递了以下参考:
FirebaseDatabase.getInstance().getReference().child("southindian")
对于“setQuery()”方法,这意味着该引用下的每个孩子都将呈现为“model1”类型的对象。这实际上是不可能的,因为在“southindian”节点下,只存在三个 String 对象,因此出现了错误。请记住,在 Java 中,您无法转换 String tp "model1" 类型的对象,因为它们之间没有继承关系。
我个人怀疑,但如果“menu”子项与“southindian”子项具有相同的属性,那么您应该传递给该方法,参考如下:
FirebaseDatabase.getInstance().getReference()
无需调用 .child("southindian")。这意味着每个孩子都将被视为“model1”类型的对象。如果不是这种情况,则删除第一个“菜单”子项或尝试创建另一个模式,其中在单个节点中仅存储相同类型的对象。
【讨论】:
menu child doesn't have the same properties as southindian class 我该怎么办? 我告诉过你,删除那个孩子并将其添加到另一个地方。或者创建另一个你应该添加的结构,只有 model1 类型的对象。 谢谢先生,教我。问题在于 model1 类中的字符串价格。因为它不是 String.Converted 长并且它有效。谢谢以上是关于com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为 com.example.gatewayrest的主要内容,如果未能解决你的问题,请参考以下文章