当前位置: 首页 > 知识库问答 >
问题:

为什么我在使用Firebase Recycerer视图适配器时得到调用目标异常?

岑畅
2023-03-14
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);

下面是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

这是NoteViewHolder文件。

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);
    }
}

共有1个答案

邓焱
2023-03-14

更新:

我在logcat中没有看到类强制转换异常:

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

您似乎正在将NoteViewholder强制转换为view$OnClickListener,这也是the Wonderer提到的,我对您的代码做了一些更改,这可能会有所帮助。

对于您的查看器,这是很好的:

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);
    }

对于getters-setters,使用model,它们应该如下所示(作为一个示例):

public final class User {

    private String mName;
    private String mEmail;

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

你可以在这里找到一个好的:https://stackoverflow.com/a/48037074/4409113

 类似资料:
  • 在旁观者(@NonNull视听者持有人,国际位置)上的公共空白{ 单击cardView时,Intent不会传递给其他活动。

  • 我使用Gson Library 2.8.0将JSON字符串转换为Java Hashmap,Maven作为构建系统,Tomcat作为WebApp运行我的代码。IDE:IntelliJ Idea 问题是每当我运行我的代码,执行停止和抛出类NotFound异常。 代码==>

  • 我正在学习回收器视图,只是构建了一个基本列表来显示名称和编号,但却遇到了运行时异常 这是密码 mainactivity.java 回收器ViewAdpater.java person.java activity_main.xml recycler_layout.xml 但我得到了这样的excpeton:e/AndroidRuntime:致命异常:主进程:com.example.ListViewan

  • 我收到一个非法监视器异常。我在谷歌上搜索了一下,但没有什么能说明我做错了什么。 从这个normalclass中,我为其他类创建了一个对象,并将该对象交给thread和同步thread。为什么我会得到这个例外?

  • 我正在学习React,并且在我的项目中安装了ESLint。它开始给我一些错误,比如: 在setState中引用之前的状态react/no access状态时,在setState中使用回调 在我的React组件中,我有一个构造函数: 在我的职能中,我有: 我理解在中使用是不正确的,但我如何解决此错误?

  • 问题是:我还没有找到一个有效的解决方案,允许我在我的recyclerview适配器中使用 我知道我的选择似乎要么是实现一个接口,要么是使用lambda,然而我一直在尝试我能找到的一切,但没有一个是有效的。 我在以下地方尝试过解决方案: 项目Onclick RecyclerView Kotlin Android 科特林的RecyclerView onClick 如何将点击监听器添加到我的回收器视图(