com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为 com.example.prj.User 类型

Posted

技术标签:

【中文标题】com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为 com.example.prj.User 类型【英文标题】:com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.prj.User 【发布时间】:2020-08-17 17:26:18 【问题描述】:

我正在尝试从 Firebase 实时数据库中检索数据,并希望在 TextView 的屏幕上显示它们,但遇到错误。 我的数据库结构是

logcat 中显示的错误是

UserFragment类的代码如下

package com.example.prj;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;


/**
 * A simple @link Fragment subclass.
 * Use the @link UserFragment#newInstance factory method to
 * create an instance of this fragment.
 */
public class UserFragment extends Fragment 
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    ImageView profile;
    TextView shopName, ownerName, address, phoneNumber , gstNumber, email, policyLink;
    DatabaseReference myRef;
    public UserFragment() 
        // Required empty public constructor
    

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment UserFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static UserFragment newInstance(String param1, String param2) 
        UserFragment fragment = new UserFragment();
        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) 
        // Inflate the layout for this fragment
        View v =  inflater.inflate(R.layout.fragment_user, container, false);
        String u = FirebaseAuth.getInstance().getCurrentUser().getUid();
        myRef = FirebaseDatabase.getInstance().getReference(u).child("User");
        profile = v.findViewById(R.id.profile_image);
        shopName = v.findViewById(R.id.owner_name);
        ownerName = v.findViewById(R.id.owner_name);
        address = v.findViewById(R.id.address);
        phoneNumber = v.findViewById(R.id.phone_number);
        gstNumber = v.findViewById(R.id.gst_number);
        email = v.findViewById(R.id.email);
        policyLink = v.findViewById(R.id.policy_link);

        myRef.addValueEventListener(new ValueEventListener() 
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) 
                for(DataSnapshot detailSnapshot:dataSnapshot.getChildren())
                
                    User user= detailSnapshot.getValue(User.class);
                    shopName.setText(user.getShopname());
                    ownerName.setText(user.getOwner());
                    address.setText(user.getAddress());
                   String p = user.getPhone1()+" , "+user.getPhone2();
                    phoneNumber.setText(p);
                    gstNumber.setText(user.getGst());
                    email.setText(user.getEmail());

                
            

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) 

            
        );
        return  v;
    




UserFragment布局的XML文件如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".UserFragment">


<ImageView
    android:layout_
    android:layout_
    android:layout_marginTop="10dp"
    android:background="@drawable/profile_pic"
    android:layout_marginRight="20dp"
    android:layout_marginLeft="20dp"
    android:id="@+id/logo"
    />
    <TextView
        android:layout_
        android:layout_
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:layout_below="@+id/logo"
        android:layout_marginTop="5dp"
        android:textSize="20dp"
        android:id="@+id/shop_name"
        />
    <TextView
        android:layout_
        android:layout_
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:layout_below="@+id/shop_name"
        android:layout_marginTop="5dp"
        android:textSize="20dp"
        android:id="@+id/owner_name"
        />
    <TextView
        android:layout_
        android:layout_
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:layout_below="@+id/owner_name"
        android:layout_marginTop="5dp"
        android:textSize="20dp"
        android:id="@+id/address"
        />
    <TextView
        android:layout_
        android:layout_
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:layout_below="@+id/address"
        android:layout_marginTop="5dp"
        android:textSize="20dp"
        android:id="@+id/phone_number"
        />
    <TextView
        android:layout_
        android:layout_
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:layout_below="@+id/phone_number"
        android:layout_marginTop="5dp"
        android:textSize="20dp"
        android:id="@+id/gst_number"
        />
    <TextView
        android:layout_
        android:layout_
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:layout_below="@+id/gst_number"
        android:layout_marginTop="5dp"
        android:textSize="20dp"
        android:id="@+id/email"
        />
    <TextView
        android:layout_
        android:layout_
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:layout_below="@+id/email"
        android:layout_marginTop="5dp"
        android:textSize="20dp"
        android:id="@+id/policy_link"
        />
</RelativeLayout>

User类的代码如下

package com.example.prj;

public class User 
    private static final String TAG = "User";
    String address, email, gst, owner, phone1, phone2, shopname;

    public User() 
    

    public User(String address, String email, String gst, String owner, String phone1, String phone2, String shopname) 
        this.address = address;
        this.email = email;
        this.gst = gst;
        this.owner = owner;
        this.phone1 = phone1;
        this.phone2 = phone2;
        this.shopname = shopname;
    

    public static String getTAG() 
        return TAG;
    

    public String getAddress() 
        return address;
    

    public void setAddress(String address) 
        this.address = address;
    

    public String getEmail() 
        return email;
    

    public void setEmail(String email) 
        this.email = email;
    

    public String getGst() 
        return gst;
    

    public void setGst(String gst) 
        this.gst = gst;
    

    public String getOwner() 
        return owner;
    

    public void setOwner(String owner) 
        this.owner = owner;
    

    public String getPhone1() 
        return phone1;
    

    public void setPhone1(String phone1) 
        this.phone1 = phone1;
    

    public String getPhone2() 
        return phone2;
    

    public void setPhone2(String phone2) 
        this.phone2 = phone2;
    

    public String getShopname() 
        return shopname;
    

    public void setShopname(String shopname) 
        this.shopname = shopname;
    

我试图在 Firebase 中显示这些子节点的值,但由于错误而无法这样做。

【问题讨论】:

【参考方案1】:

在路径/$uid/User 中,您保留了单个用户的数据,因此当您读取该数据时,您将获得包含单个用户数据的快照。但是您的 onDataChange 会循环结果,就好像有多个用户一样。你应该删除这个循环,所以:

myRef.addValueEventListener(new ValueEventListener() 
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) 
        User user= dataSnapshot.getValue(User.class);
        ...

【讨论】:

以上是关于com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为 com.example.prj.User 类型的主要内容,如果未能解决你的问题,请参考以下文章