Firestore回收器适配器中的Getitem方法返回null

Firestore回收器适配器中的Getitem方法返回null

本文介绍了Firestore回收器适配器中的Getitem方法返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我得到的错误:

FATAL EXCEPTION: main
 Process: neoncore.com.onepiece, PID: 19429
                                                                           java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[])' on a null object reference
                                                                               at com.google.android.gms.internal.zzevb$zza.zza(Unknown Source)
                                                                               at com.google.android.gms.internal.zzevb.zza(Unknown Source)
                                                                               at com.google.android.gms.internal.zzevb.zza(Unknown Source)
                                                                               at com.google.firebase.firestore.DocumentSnapshot.toObject(Unknown Source)
                                                                               at com.firebase.ui.firestore.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:23)
                                                                               at com.firebase.ui.firestore.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:12)
                                                                               at com.firebase.ui.common.BaseCachingSnapshotParser.parseSnapshot(BaseCachingSnapshotParser.java:35)
                                                                               at com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:52)
                                                                               at com.firebase.ui.firestore.FirestoreRecyclerAdapter.getItem(FirestoreRecyclerAdapter.java:83)
                                                                               at neoncore.com.onepiece.activities.ChatDetail$3.getItem(ChatDetail.java:168)
                                                                               at neoncore.com.onepiece.activities.ChatDetail$3.getItemViewType(ChatDetail.java:174)
                                                                               at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5657)
                                                                               at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5589)
                                                                               at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5585)
                                                                               at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2231)
                                                                               at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1558)
                                                                               at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1518)
                                                                               at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585)
                                                                               at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3719)
                                                                               at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3436)
                                                                               at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3988)
                                                                               at android.view.View.layout(View.java:16694)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5481)
                                                                               at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1514)
                                                                               at android.view.View.layout(View.java:16694)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5481)
                                                                               at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1171)
                                                                               at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:856)
                                                                               at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:875)
                                                                               at android.view.View.layout(View.java:16694)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5481)
                                                                               at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                               at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                               at android.view.View.layout(View.java:16694)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5481)
                                                                               at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                               at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                               at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                               at android.view.View.layout(View.java:16694)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5481)

当我在调试模式下运行应用程序时,我可以看到getItemcount方法的大小增加了,这意味着我正在获取数据,但随后会产生上述错误.上面的错误指向getItem方法,该方法为null,这是我的Firestore Recycler适配器:

When i ran the application in debug mode, i could see the getItemcount method size is increased, which means i am getting the data but it then produces the error above. The error above points to the getItem method which is null, This is my firestore recycler adapter:

public void populateMessageView(Query query){



 FirestoreRecyclerOptions<Message> firestoreRecyclerOptions = new FirestoreRecyclerOptions.Builder<Message>()
            .setQuery(query,Message.class)
            .build();
    recyclerAdapter = new FirestoreRecyclerAdapter<Message,RecyclerView.ViewHolder>(firestoreRecyclerOptions) {


        @Override
        protected void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull Message model) {

            switch (holder.getItemViewType()) {
                case VIEW_TYPE_MESSAGE_SENT:
                    ((MeMessageHolder) holder).bind(model);
                    break;
                case VIEW_TYPE_MESSAGE_RECEIVED:
                    ((OtherMessageHolder) holder).bind(model);
            }

        }

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

        @NonNull
        @Override
        public Message getItem(int position) {
            return super.getItem(position);

        }

        @Override
        public int getItemViewType(int position) {
            Message message = getItem(position);
            if(author.getId() == message.getAuthor().getId()){
                return VIEW_TYPE_MESSAGE_SENT;
            }
            else return VIEW_TYPE_MESSAGE_RECEIVED;
            //return super.getItemViewType(position);
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
           View view;
            if (viewType == VIEW_TYPE_MESSAGE_SENT) {
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_item_chat_me, parent, false);
                return new MeMessageHolder(view);
            } else if (viewType == VIEW_TYPE_MESSAGE_RECEIVED) {
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_item_chat_other, parent, false);
                return new OtherMessageHolder(view);
            }

            return null;

        }

        @Override
        public void onError(@NonNull FirebaseFirestoreException e) {
            super.onError(e);
            Log.d(TAG, e.getLocalizedMessage());
        }

    };



    messagesList.setAdapter(recyclerAdapter);
    recyclerAdapter.notifyDataSetChanged();

}

private class MeMessageHolder extends RecyclerView.ViewHolder{
    private TextView messageBody, messageTime;
    SimpleDateFormat simpleDateFormat ;

    public MeMessageHolder(View itemView) {
        super(itemView);
        messageBody = findViewById(R.id.text_message_body_other);
        messageTime = findViewById(R.id.text_message_time);
        simpleDateFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
    }
    void bind (Message message){

        messageBody.setText(message.getText());
        messageTime.setText(simpleDateFormat.format(message.getTimestamp()));

    }
}

private class OtherMessageHolder extends RecyclerView.ViewHolder{
    private TextView messageBody,messageTime,messageName;
    private ImageView otherMessageImage;
    SimpleDateFormat simpleDateFormat;
    public OtherMessageHolder(View itemView) {
        super(itemView);
        messageBody = findViewById(R.id.text_message_body_other);
        messageTime = findViewById(R.id.text_message_time_other);
        messageName = findViewById(R.id.text_message_name);
        otherMessageImage = findViewById(R.id.image_message_profile);
        simpleDateFormat = new SimpleDateFormat("HH:mm",Locale.getDefault());

    }

    void bind (Message message){
        messageName.setText(message.getAuthor().getName());
        messageBody.setText(message.getText());
        messageTime.setText(simpleDateFormat.format(message.getTimestamp()));

    }
}

我为此适配器使用了两个视口.

I am using two viewholders for this adapter.

推荐答案

我发现了导致此错误的两个问题...1.我的消息类没有默认的构造函数.2.我的观察者使用的是findviewbyid而不是itemview.findviewbyid ...基本上...错误是我的错误导致的..很高兴我发现了..

I figured out a couple of problems that was causing this error...1.my message class did not have a default constructor.2.my viewholders were using findviewbyid instead of itemview.findviewbyid...Basically...the error was as a result of my mistake..glad i figured it out..

这篇关于Firestore回收器适配器中的Getitem方法返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 15:29