Java.lang.InstantiationException, instantiating RecyclerView.ViewHolder
I inflate two types of views for chat, in my other activity called ActivityA works fine, but in my activity B I use the same process but it doesn't work.
The problem arises in the OnCreateView holder In the "return super.onCreateViewHolder (parent, viewType);" Relevant code:
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;
}
}
};
}
my 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);
}
}
an exception:
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)
I don't know what it might be, as in my activity it works wonderfully
source to share
There is a problem with this method: -
@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 with anyway, your onCreateViewHolder file is required to create the view owner from the parent. Your parent has a ViewHolder class type for RecyclerView.ViewHolder , which is abstract so it cannot be initialized. You have to make sure the ViewHolder is always created by your implementation, or you can do like this: -
@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);
}
}
Or a better solution. First, you need to override
@Override
public int getItemViewType(int position) {
//Always return from handle type in switch block ** onCreateViewHolder**
return CHAT_RIGHT || CHAT_LEFT;
}
Make sure you always get a type like CHAT_RIGHT or CHAT_LEFT . Hope this solves your problem.
source to share