到达RecyclerView Android末尾时,应用程序停止了

时间:2019-06-21 06:51:27

标签: java android firebase android-recyclerview

当我在RecyclerView中滚动浏览卡片时,该应用程序运行良好,但是当RecyclerView到达最后一张卡片时,该应用程序崩溃了;这意味着当我到达RecyclerView底部时,该应用程序一直崩溃。 日志猫说

com.google.firebase.database.DatabaseException:

无法将java.lang.String类型的对象转换为com.pappu5.navigation.PostActivity类型

有什么方法可以解决这个问题? 提前致谢 :) 日志

06-21 12:08:19.799 21292-21292/com.pappu5.navigation E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.pappu5.navigation, PID: 21292
    com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.pappu5.navigation.PostActivity
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@17.0.0:418)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@17.0.0:214)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@17.0.0:79)
        at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@17.0.0:212)
        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:6673)
        at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714)
        at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)
        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)
        at android.support.v7.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:285)
        at android.support.v7.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:342)
        at android.support.v7.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:358)
        at android.support.v7.widget.GapWorker.prefetch(GapWorker.java:365)
        at android.support.v7.widget.GapWorker.run(GapWorker.java:396)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

PostActivity.java

package com.pappu5.navigation;

import com.google.firebase.firestore.ServerTimestamp;
import java.util.Date;

public class PostActivity {
    public String image,thumb_image,desc,user;

    //public Date timestamp;
    public PostActivity(){

    }


    public PostActivity(String image, String thumb_image, String desc, String user) {
        this.image = image;
        this.thumb_image = thumb_image;
        this.desc = desc;
        this.user = user;
        //this.timestamp = timestamp;

    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getThumb_image() {
        return thumb_image;
    }

    public void setThumb_image(String thumb_image) {
        this.thumb_image = thumb_image;
    }

    public String getDesc() {
        if(desc!=null){
            return desc;
        }else{
            return "This is Null";
        }

    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    /*public Date getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }*/

}



HomeFragment.java



public class HomeFragment extends Fragment {

    private RecyclerView rv;
    private TextView username,date,desc;
    private CircleImageView postImage;
    private ImageView postBigImage;
    private CardView postCard;
    private List<PostActivity> postItems;
    private DatabaseReference dr;
    private StorageReference sr;
    private FirebaseUser user;
    FirebaseRecyclerAdapter<PostActivity, PostsHolder> fra;


private PostAdapter pa;

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



   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);


        sr = FirebaseStorage.getInstance().getReference();
        user = FirebaseAuth.getInstance().getCurrentUser();
        String id = user.getUid();

        AddPost ap = new AddPost();

        dr = FirebaseDatabase.getInstance().getReference().child("Posts");

        postItems = new ArrayList<>();
        pa = new PostAdapter(postItems);

        rv =view.findViewById(R.id.rvPostItems);

        rv.setLayoutManager(new LinearLayoutManager(getActivity()));


       Query personsQuery = dr.orderByKey();

       FirebaseRecyclerOptions<PostActivity> options =
               new FirebaseRecyclerOptions.Builder<PostActivity>().setLifecycleOwner(this)
                       .setQuery(personsQuery, PostActivity.class)
                       .build();

       fra = new FirebaseRecyclerAdapter<PostActivity, PostsHolder>(
               options
       ) {


           @Override
           protected void onBindViewHolder(@NonNull PostsHolder holder, int position, @NonNull PostActivity model) {
               holder.setDesc(model.getDesc());
           }

           @NonNull
           @Override
           public PostsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {


               View view = LayoutInflater.from(parent.getContext())
                       .inflate(R.layout.post_items, parent, false);

               return new PostsHolder(view);
           }
       };

       rv.setAdapter(fra);

        return view;
    }





    public  class PostsHolder extends RecyclerView.ViewHolder{

        View view;
        private TextView de;
        public PostsHolder(View itemView) {
            super(itemView);

            view=itemView;

        }

        public void setDesc(String descText){

            de = view.findViewById(R.id.postDesc);

            de.setText(descText);

        }

    }




    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }

}







1 个答案:

答案 0 :(得分:0)

Ogbe评论的几乎全部。

如错误消息所述,问题是您试图将数据库中的字符串转换为PostActivity,这是不可能的。

Posts下列表中的数据中,某处很可能只有一个字符串值。删除它,问题应该消失。

这也许是查看Firebase's security rules的好时机,因为验证规则是防止此类格式错误的数据的好方法。