firestore循环器适配器中的Getitem方法返回null

时间:2018-02-22 21:00:31

标签: android firebase android-recyclerview google-cloud-firestore firebaseui

这是我得到的错误:

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 recycleler 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()));

    }
}

我正在为此适配器使用两个视图。

0 个答案:

没有答案