android 无法将java.lang.String类型的值转换为int(在explore_fragment中)

jgwigjjp  于 2022-12-16  发布在  Android
关注(0)|答案(1)|浏览(123)

Showing error on getValue(User.class) in explore_fragment. My explore_fragment is:

public class exploreFragment extends Fragment {
    FragmentExploreBinding binding;
    ArrayList<User> list = new ArrayList<>();
    FirebaseAuth auth;
    FirebaseDatabase database;

    public exploreFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        auth = FirebaseAuth.getInstance();
        database= FirebaseDatabase.getInstance();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding= FragmentExploreBinding.inflate(inflater, container, false);
        UserAdapter adapter = new UserAdapter(getContext(),list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        binding.usersRV.setLayoutManager(layoutManager);
        binding.usersRV.setAdapter(adapter);

        database.getReference().child("Users").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                list.clear();
                for(DataSnapshot dataSnapshot : snapshot.getChildren()){
                    User user = dataSnapshot.getValue(User.class);
                    user.setUserID(dataSnapshot.getKey());

                    if(!dataSnapshot.getKey().equals(FirebaseAuth.getInstance().getUid())){
                        list.add(user);
                    }
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

        return binding.getRoot();
    }
}

User_model is:

public class User {
    private String name, profession,email,password,cname;
    private String profile;
    private String userReff;
    private int guidedCount;
    private String userID;
    private  String coverPhoto;

    public User() {
    }

    public int getGuidedCount() {
        return guidedCount;
    }
    public void setGuidedCount(int guidedCount) {
        this.guidedCount = guidedCount;
    }

    public String getCoverPhoto() {
        return coverPhoto;
    }
    public void setCoverPhoto(String coverPhoto) {
        this.coverPhoto = coverPhoto;
    }

    public User(String name, String profession, String email, String password, String cname) {
        this.name = name;
        this.profession = profession;
        this.email = email;
        this.password = password;
        this.cname = cname;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getProfession() {
        return profession;
    }
    public void setProfession(String profession) {
        this.profession = profession;
    }

    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public String getCname() {
        return cname;
    }
    public void setCname(String cname) {
        this.cname = cname;
    }

    public String getProfile() {
        return profile;
    }
    public void setProfile(String profile) {
        this.profile = profile;
    }

    public String getUserReff() {
        return userReff;
    }
    public void setUserReff(String userReff) {
        this.userReff = userReff;
    }

    public String getUserID() {
        return userID;
    }

    public void setUserID(String userID) {
        this.userID = userID;
    }
}

User_adapter is:

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.viewHolder>{
    Context context;
    ArrayList<User> list;

    public UserAdapter(Context context, ArrayList<User> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.user_sample,parent,false);
        return new viewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull viewHolder holder, int position) {
        User user = list.get(position);
        Picasso.get().load(user.getProfile()).placeholder(R.drawable.placeholder).into(holder.binding.profileImage);
        holder.binding.name.setText(user.getName());
        holder.binding.profession.setText(user.getProfession());

        holder.binding.viewprofilebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view){

                String visiter = list.get(holder.getAdapterPosition()).getUserID();
                Intent intent= new Intent(context, VActivity.class);
                intent.putExtra("usersid",visiter).toString();
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class viewHolder extends RecyclerView.ViewHolder{
        UserSampleBinding binding;
        public viewHolder(@NonNull View itemView) {
            super(itemView);

            binding = UserSampleBinding.bind(itemView);
        }
    }
}

Error i am getting is:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.guided_app, PID: 2744 com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to int at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertInteger(CustomClassMapper.java:364) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToPrimitive(CustomClassMapper.java:290) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:215) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(CustomClassMapper.java:179) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$100(CustomClassMapper.java:48) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:593) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:563) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:433) 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.example.guided_app.fragment.exploreFragment$1.onDataChange(exploreFragment.java:60) at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75) at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63) at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
The firebase database is:

"Users": {
    "59fLuGGNugPcgp6b725cFnKIzKC2": {
      "cname": "LIT",
      "email": "Stephen@gmail.com",
      "guidedCount": 0,
      "name": "Stephen Wade",
      "password": "123456",
      "profession": "Developer @ Adobe"
    },
    "7kpNGqcHeBfNqf8GrVK2Hpew0L62": {
      "cname": "BIT",
      "email": "anshul@gmail.com",
      "guided": {
        "FOQEVbKRpNYjfzAJCp1XQtnvRlh2": {
          "guidedAt": 1670152487063,
          "guidedBy": "FOQEVbKRpNYjfzAJCp1XQtnvRlh2"
        },
        "y3pV1GhdLqOnteMO64U2F4o8mMu2": {
          "guidedAt": 1670151228825,
          "guidedBy": "y3pV1GhdLqOnteMO64U2F4o8mMu2"
        }
      },
      "guidedCount": 2,
      "name": "Anshul Lanjewar",
      "password": "123456",
      "profession": "SDE @ Google"
    },
    "FOQEVbKRpNYjfzAJCp1XQtnvRlh2": {
      "cname": "SIT",
      "email": "Tanvi@gmail.com",
      "guidedCount": 0,
      "name": "Tanvi Colson",
      "password": "123456",
      "profession": "Analyst @ Google"
    },
    "Jj2RH3iopgdLU6AC3VKeeaMKAXx1": {
      "cname": "PIT",
      "email": "Shana@gmail.com",
      "guidedCount": 0,
      "name": "Shana Sharma",
      "password": "123456",
      "profession": "FullStack @ Netflix"
    },
    "gAzcrP1IYmQI0ht4qfH9WGt9U7F2": {
      "cname": "MIT",
      "email": "John@gmail.com",
      "guided": {
        "7kpNGqcHeBfNqf8GrVK2Hpew0L62": {
          "guidedAt": 1670614050015,
          "guidedBy": "7kpNGqcHeBfNqf8GrVK2Hpew0L62"
        }
      },
      "guidedCount": "gAzcrP1IYmQI0ht4qfH9WGt9U7F2",
      "name": "John Adams",
      "password": "123456",
      "profession": "Developer @ Apple"
    },
    "y3pV1GhdLqOnteMO64U2F4o8mMu2": {
      "cname": "BIT",
      "email": "kumar@gmail.com",
      "guided": {
        "7kpNGqcHeBfNqf8GrVK2Hpew0L62": {
          "guidedAt": 1670154254299,
          "guidedBy": "7kpNGqcHeBfNqf8GrVK2Hpew0L62"
        }
      },
      "guidedCount": 1,
      "name": "Kumar Mishra",
      "password": "123456",
      "profession": "SDE @ Microsoft"
    }
  }

I recently started with android development, so was just stucked. I want to create an explore_fragment in which list of users is added in recyclerView and with view_profile button we can view user profile.

lrl1mhuk

lrl1mhuk1#

如预期,出现以下错误:
com.google.firebase.database.DatabaseException:无法将类型为java.lang.String的值转换为int
由于int类型字段包含String类型的值而到达。因此,在反序列化时,guidedCount字段应包含int类型的值,而不是String类型的值,因此出现错误。导致此异常的节点为:

"guidedCount": "gAzcrP1IYmQI0ht4qfH9WGt9U7F2",

看,它保存的是一个UID而不是一个数字。所以要解决这个问题,用一个数字来更改UID。

相关问题