java.lang.InstantiationException,实例化RecyclerView.ViewHolder

时间:2017-06-12 03:44:08

标签: java android android-recyclerview recycler-adapter android-viewholder

我正在为聊天提供两种类型的视图,在我的其他名为ActivityA的活动中工作得很好但是,在我的活动B中我使用相同的过程但它不起作用

OnCreateView持有者出现问题 在'return super.onCreateViewHolder(parent,viewType);'的部分 相关代码:

     private void setUpAdapter(){

    mAdapter = new FirebaseRecyclerAdapter<GroupalChat, RecyclerView.ViewHolder>(GroupalChat.class, R.layout.dumb_layout, RecyclerView.ViewHolder.class, FirebaseUtils.getGrupalChatRef(mPost.getId())) {
        @Override
        protected void populateViewHolder(RecyclerView.ViewHolder viewHolder, GroupalChat model, int position) {
            switch(model.getType()){
                case CHAT_RIGHT:
                    populateViewHolder((ChatGroupalHolderSender) viewHolder, model ,position);
                    break;
                case CHAT_LEFT:
                    populateViewHolder((ChatGroupalHolderGetter) viewHolder, model ,position);
                    break;
            }
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            switch(viewType){

                case CHAT_RIGHT:
                    View sender = LayoutInflater.from(getApplicationContext()).inflate(R.layout.chat_grupal_sender, parent, false);
                    return new ChatGroupalHolderSender(sender);
                case CHAT_LEFT:
                    View left = LayoutInflater.from(getApplicationContext()).inflate(R.layout.chat_grupal_getter, parent, false);
                    return new ChatGroupalHolderGetter(left);
            }
            return super.onCreateViewHolder(parent, viewType);

        }

        @Override
        public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, int position) {
            GroupalChat item = getItem(position);
            int itemViewType = getItemViewType(position);
            switch (itemViewType){
                case CHAT_RIGHT:
                    FirebaseUtils.getUsuarioReferencia(firebaseUser.getUid()).child(firebaseUser.getUid()).child("username").addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if(dataSnapshot.getValue(String.class) != null){
                                String name = dataSnapshot.getValue(String.class);
                                ((ChatGroupalHolderSender) viewHolder).mNombrePerfil.setText(name);
                            }

                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });

                    FirebaseUtils.getUsuarioReferencia(firebaseUser.getUid()).child(firebaseUser.getUid()).child("photoUrl").addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if(dataSnapshot.getValue(String.class) != null){
                                String url = dataSnapshot.getValue(String.class);
                                StorageReference storageReference = FirebaseStorage.getInstance().getReference(url);
                                Glide.with(GroupChatActivity.this).using(new FirebaseImageLoader()).load(storageReference).into(((ChatGroupalHolderSender) viewHolder).mFotoPerfil);
                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });

                    ((ChatGroupalHolderSender) viewHolder).mTexto.setText(item.getMessage());
                    ((ChatGroupalHolderSender) viewHolder).mFecha.setText(DateUtils.getRelativeTimeSpanString(item.getTiempoMensaje()));
                    break;

                // GETTER
                case CHAT_LEFT:
                    FirebaseUtils.getUsuarioReferencia(item.getUID()).child(item.getUID()).child("username").addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if(dataSnapshot.getValue(String.class) != null){
                                String name = dataSnapshot.getValue(String.class);
                                ((ChatGroupalHolderGetter) viewHolder).mNombrePerfil.setText(name);
                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });

                    FirebaseUtils.getUsuarioReferencia(item.getUID()).child(item.getUID()).child("photoUrl").addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if(dataSnapshot.getValue(String.class) != null){
                                String url = dataSnapshot.getValue(String.class);
                                StorageReference storageReference = FirebaseStorage.getInstance().getReference(url);
                                Glide.with(GroupChatActivity.this).using(new FirebaseImageLoader()).load(storageReference).into(((ChatGroupalHolderGetter) viewHolder).mFotoPerfil);
                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });
                    ((ChatGroupalHolderGetter) viewHolder).mTexto.setText(item.getMessage());
                    ((ChatGroupalHolderGetter) viewHolder).mFecha.setText(DateUtils.getRelativeTimeSpanString(item.getTiempoMensaje()));
                    break;

            }

        }




    };

}

我的ViewHolders

  public static class ChatGroupalHolderGetter extends RecyclerView.ViewHolder{

    private CircleImageView mFotoPerfil;
    private TextView mNombrePerfil, mFecha, mTexto;

    public ChatGroupalHolderGetter(View itemView) {
        super(itemView);

        mFotoPerfil = (CircleImageView) itemView.findViewById(R.id.fotoPerfilChatGrupalG);

        mNombrePerfil = (TextView) itemView.findViewById(R.id.nombreUsuarioChatGrupalG);
        mFecha = (TextView) itemView.findViewById(R.id.horaChatGrupalG);
        mTexto = (TextView) itemView.findViewById(R.id.textoDeChatGrupalG);


    }
}



public static class ChatGroupalHolderSender extends RecyclerView.ViewHolder{

    private CircleImageView mFotoPerfil;
    private TextView mNombrePerfil, mFecha, mTexto;

    public ChatGroupalHolderSender(View itemView) {
        super(itemView);

        mFotoPerfil = (CircleImageView) itemView.findViewById(R.id.fotoPerfilChatGrupal);

        mNombrePerfil = (TextView) itemView.findViewById(R.id.nombreUsuarioChatGrupal);
        mFecha = (TextView) itemView.findViewById(R.id.horaChatGrupal);
        mTexto = (TextView) itemView.findViewById(R.id.textoDeChatGrupal);

    }
}

例外:

   FATAL EXCEPTION: main
                                                                      Process: com.octacorp.octa.strank, PID: 22549
                                                                      Theme: themes:{}
                                                                      java.lang.RuntimeException: java.lang.InstantiationException: Can't instantiate abstract class android.support.v7.widget.RecyclerView$ViewHolder
                                                                          at com.firebase.ui.database.FirebaseRecyclerAdapter.onCreateViewHolder(FirebaseRecyclerAdapter.java:176)
                                                                          at com.octacorp.octa.strank.ui.parties.parties.GroupChatActivity$4.onCreateViewHolder(GroupChatActivity.java:170)
                                                                          at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6367)
                                                                          at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5555)
                                                                          at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
                                                                          at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
                                                                          at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
                                                                          at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
                                                                          at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
                                                                          at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:570)
                                                                          at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
                                                                          at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3025)
                                                                          at android.view.View.measure(View.java:18799)
                                                                          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                          at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
                                                                          at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
                                                                          at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                          at android.view.View.measure(View.java:18799)
                                                                          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                          at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                          at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                                                                          at android.view.View.measure(View.java:18799)
                                                                          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                          at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
                                                                          at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
                                                                          at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                          at android.view.View.measure(View.java:18799)
                                                                          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                          at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                          at android.view.View.measure(View.java:18799)
                                                                          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                          at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
                                                                          at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
                                                                          at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                          at android.view.View.measure(View.java:18799)
                                                                          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                          at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                          at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2899)
                                                                          at android.view.View.measure(View.java:18799)
                                                                          at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2108)
                                                                          at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1224)
                                                                          at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1460)
                                                                          at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
                                                                          at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6023)
                                                                          at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                                          at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                          at android.view.Choreographer.doFrame(Choreographer.java:606)
                                                                          at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                                          at android.os.Handler.handleCallback(Handler.java:739)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          at android.os.Looper.loop(Looper.java:148)

我不知道它是什么,因为在我的activtyA中,它的运作非常好

1 个答案:

答案 0 :(得分:2)

此方法存在问题: -

@Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            switch(viewType){

                case CHAT_RIGHT:
                    View sender = LayoutInflater.from(getApplicationContext()).inflate(R.layout.chat_grupal_sender, parent, false);
                    return new ChatGroupalHolderSender(sender);
                case CHAT_LEFT:
                    View left = LayoutInflater.from(getApplicationContext()).inflate(R.layout.chat_grupal_getter, parent, false);
                    return new ChatGroupalHolderGetter(left);
            }
            **return super.onCreateViewHolder(parent, viewType);**

        }

As在哪种情况下,您的onCreateViewHolder需要从父级创建视图持有者。 您的父级具有 RecyclerView.ViewHolder 的ViewHolder类类型,这是抽象的,因此无法初始化。您需要确保ViewHolder始终由您的实现创建,您可以这样做: -

@Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch(viewType){

            case CHAT_RIGHT:
                View sender = LayoutInflater.from(getApplicationContext()).inflate(R.layout.chat_grupal_sender, parent, false);
                return new ChatGroupalHolderSender(sender);
            default:
                View left = LayoutInflater.from(getApplicationContext()).inflate(R.layout.chat_grupal_getter, parent, false);
                return new ChatGroupalHolderGetter(left);
        }


    }

或者更好的解决方案首先,您需要覆盖

@Override
    public int getItemViewType(int position) {
        //Always return from handle type in switch block ** onCreateViewHolder**
        return CHAT_RIGHT || CHAT_LEFT;
    }

确保您始终获得 CHAT_RIGHT CHAT_LEFT 的观看类型。 希望它能解决你的问题。