1
FirebaseRecyclerAdapter<NoteModel, NoteViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<NoteModel, NoteViewHolder>(
        NoteModel.class,
        R.layout.note_card,
        NoteViewHolder.class,
        fNotesDatabase) {
    @Override
    protected void populateViewHolder(final NoteViewHolder viewHolder, NoteModel model, int position) {
        final String noteId = getRef(position).getKey();

        Log.e("MainActivity","Populate View");

        fNotesDatabase.child(noteId).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.hasChild("mTitle") && dataSnapshot.hasChild("mTime")) {
                    String title = dataSnapshot.child("mTitle").getValue().toString();
                    String timestamp = dataSnapshot.child("mTime").getValue().toString();
                    String content =  dataSnapshot.child("mContent").getValue().toString();

                    GetTimeAgo getTimeAgo = new GetTimeAgo();
                    viewHolder.setTime(getTimeAgo.getTimeAgo(Long.parseLong(timestamp), getApplicationContext()));

                    viewHolder.setTitle(title);
                    //viewHolder.setNoteTime(timestamp);
                    viewHolder.setContent(content);
                    viewHolder.setTime(timestamp);
                    Log.e("MainActivity","Data retrieval"+title);

                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }
};
recyclerView.setAdapter(firebaseRecyclerAdapter);

This is inside a function named loadData() in my MainActivity why am I getting the InvocationTargetException?

The Firebase Instance is declared in the onCreate() function of the MainActivity.

Where's the problem, because I'm guessing I've added all the necessary code.

Here's the logcat:

09-29 19:46:31.266 7997-7997/com.dell.noteit E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dell.noteit, PID: 7997
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at com.firebase.ui.database.FirebaseRecyclerAdapter.onCreateViewHolder(FirebaseRecyclerAdapter.java:202)
    at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6493)
    at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5680)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)
    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
    at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:556)
    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)
    at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)
    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410)
    at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1710)
    at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:346)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:959)
    at android.view.Choreographer.doCallbacks(Choreographer.java:734)
    at android.view.Choreographer.doFrame(Choreographer.java:667)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:945)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6776)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
    at com.firebase.ui.database.FirebaseRecyclerAdapter.onCreateViewHolder(FirebaseRecyclerAdapter.java:198)
    at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6493) 
    at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5680) 
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563) 
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559) 
    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229) 
    at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:556) 
    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516) 
    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608) 
    at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170) 
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693) 
    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410) 
    at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1710) 
    at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:346) 
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:959) 
    at android.view.Choreographer.doCallbacks(Choreographer.java:734) 
    at android.view.Choreographer.doFrame(Choreographer.java:667) 
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:945) 
    at android.os.Handler.handleCallback(Handler.java:751) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6776) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386) 
 Caused by: java.lang.ClassCastException: com.dell.noteit.NoteViewHolder cannot be cast to android.view.View$OnClickListener
    at com.dell.noteit.NoteViewHolder.<init>(NoteViewHolder.java:25)
    at java.lang.reflect.Constructor.newInstance0(Native Method) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:430) 
    at com.firebase.ui.database.FirebaseRecyclerAdapter.onCreateViewHolder(FirebaseRecyclerAdapter.java:198) 
    at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6493) 
    at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5680) 
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563) 
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559) 
    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229) 
    at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:556) 
    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516) 
    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608) 
    at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170) 
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693) 
    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410) 
    at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1710) 
    at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:346) 
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:959) 
    at android.view.Choreographer.doCallbacks(Choreographer.java:734) 
    at android.view.Choreographer.doFrame(Choreographer.java:667) 
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:945) 
    at android.os.Handler.handleCallback(Handler.java:751) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6776) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at 

com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496) 

            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386) 
    09-29 19:46:31.317 7997-7997/com.dell.noteit I/Process: Sending signal. PID: 7997 SIG: 9

Here's the NoteViewHolder File.

NoteViewHolder.java

public class NoteViewHolder extends RecyclerView.ViewHolder
{
    public  ImageView image;
    public int imageView;
    public TextView title;
    public TextView content;
    public String color;
    public  TextView time;
    public View mview;
    public CardView notecard;


    public NoteViewHolder(View itemView) {
        super(itemView);
        mview = itemView;
        itemView.setOnClickListener((View.OnClickListener) this);
        title = itemView.findViewById(R.id.note_title);
        content = itemView.findViewById(R.id.note_content);
        time = itemView.findViewById(R.id.note_timestamp);
        image = itemView.findViewById(R.id.note_image);
        notecard = itemView.findViewById(R.id.card_view);
    }

    public String getTitle() {
        return title.getText().toString();
    }

    public void setTitle(String title) {
        this.title.setText((CharSequence) title);
    }

    public String getContent() {
        return content.getText().toString();
    }

    public void setContent(String content) {
        this.content.setText((CharSequence) content);
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getTime() {
        return time.getText().toString();
    }

    public void setTime(String time) {
        this.time.setText(time);
    }
}
Hossein Golshani
  • 1,847
  • 5
  • 16
  • 27
Wave Metric
  • 129
  • 1
  • 3
  • 13

2 Answers2

3

You're casting your NoteViewHolder to an OnClickListener. You can't do that, because it doesn't implement that class.

If you want to use this for the OnClickListener, add implements View.OnClickListener after extends RecyclerView.ViewHolder. Then remove the casting when you set the listener.

You'll also need:

@Override
public void onClick(View v) {
    //What you want to happen on the click event
}

In your ViewHolder class.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
TheWanderer
  • 16,775
  • 6
  • 49
  • 63
2

Update:

I didn't see class cast exception in the logcat:

java.lang.ClassCastException: com.dell.noteit.NoteViewHolder cannot be cast to android.view.View$OnClickListener

It seems like you're casting NoteViewHolder to View$OnClickListener which mentioned by the Wonderer too and I've made some changes to your codes which might help.

This would be fine for your ViewHolder:

public class NoteViewHolder extends RecyclerView.ViewHolder
{
    private ImageView image;   /// make all of them private
    private TextView title;
    private TextView content;
    private String color;
    private TextView time;
    private View mview;
    private CardView notecard;


    public NoteViewHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener((View.OnClickListener) this); // Here might cause issues too!
        title = itemView.findViewById(R.id.note_title);
        content = itemView.findViewById(R.id.note_content);
        time = itemView.findViewById(R.id.note_timestamp);
        image = itemView.findViewById(R.id.note_image);
        notecard = itemView.findViewById(R.id.card_view);
    }

For getters-setters, use model and those should be like this (As an example):

public final class User {

    private String mName;
    private String mEmail;

    public String getTitle() { return mName; }
    ...
    ..

You can find a good one in here: https://stackoverflow.com/a/48037074/4409113

Android Studio creates them automatically by using Alt+ Insert for Windows: How to automatically generate getters and setters in Android Studio

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
  • Their ViewHolder doesn't implement OnClickListener, but they're casting a `this` argument to it. Hence the ClassCastException farther down the log. See my answer. – TheWanderer Sep 29 '18 at 14:43
  • I don't really see any other issues. The getters and setters seem fine to me. I am on my phone though. – TheWanderer Sep 29 '18 at 14:49
  • I don't see how any of these would cause errors. setText takes a CharSequence and String implements that. It's just a redundant cast. Same for mView: just redundant. imageView is unused. image is the ImageView variable. – TheWanderer Sep 29 '18 at 16:48
  • Oh my mistake. I didn't actually see-check that *class cast exception*. However, Updated the answer. – ʍѳђઽ૯ท Sep 29 '18 at 16:56