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

项目的回收器视图上的空指针异常单击监听器活动在项目被单击时关闭

燕烨
2023-03-14
2020-10-29 16:15:14.953 21026-21026/? E/gamesdownloade: Unknown bits set in runtime_flags: 0x8000
2020-10-29 16:15:14.977 21026-21026/? E/libc: Access denied finding property "persist.sys.theme"
2020-10-29 16:15:15.041 21026-21055/? E/Perf: Fail to get file list com.downloadfrom.psp.gamesdownloader
2020-10-29 16:15:15.042 21026-21055/? E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2020-10-29 16:15:15.042 21026-21055/? E/Perf: Fail to get file list com.downloadfrom.psp.gamesdownloader
2020-10-29 16:15:15.042 21026-21055/? E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2020-10-29 16:15:24.376 21026-21026/com.downloadfrom.psp.gamesdownloader E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.downloadfrom.psp.gamesdownloader, PID: 21026
    java.lang.NullPointerException: Attempt to invoke interface method 'void com.downloadfrom.psp.gamesdownloader.ViewHolder$ClickListener.onItemClick(android.view.View, int)' on a null object reference
        at com.downloadfrom.psp.gamesdownloader.ViewHolder$1.onClick(ViewHolder.java:23)
        at android.view.View.performClick(View.java:7259)
        at android.view.View.performClickInternal(View.java:7221)
        at android.view.View.access$3800(View.java:821)
        at android.view.View$PerformClick.run(View.java:27716)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:227)
        at android.app.ActivityThread.main(ActivityThread.java:7842)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)
    null
package com.example.my.app;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;

import com.squareup.picasso.Picasso;

public class ViewHolder extends RecyclerView.ViewHolder {

    View view;

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

        view = itemView;

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mClickListener.onItemClick(view,getAdapterPosition());
            }
        });

        itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {

                mClickListener.onItemLongClick(view, getAdapterPosition());

                return true;
            }
        });
    }

    public void setDetails(Context ctx, String title, String image,String download,String size, String discription){

        TextView GameName;
        TextView Size,Download,Discription;
        ImageView GameImage;

        Size = view.findViewById(R.id.Getsize);
        Download = view.findViewById(R.id.Getdownload);
        Discription = view.findViewById(R.id.Getdiscription);

        GameName = view.findViewById(R.id.title_item);
        GameImage = view.findViewById(R.id.thumbnail_item);

        GameName.setText(title);
        Picasso.get().load(image).into(GameImage);
        Size.setText(size);
        Download.setText(download);
        Discription.setText(discription);


    }

    private ViewHolder.ClickListener mClickListener;

    public interface ClickListener{

        void onItemClick(View view,int position);
        void onItemLongClick(View view,int position);
    }

    public void setOnClickListener(ViewHolder.ClickListener clickListener){

        mClickListener = clickListener;

    }

}

这是我在开始方法的主要活动,在这个方法中,我正在从firebase在回收器视图中显示数据,也在单击listener这里设置,请检查

//MainActivity On start Method

@Override
    protected void onStart() {
        super.onStart();
        FirebaseRecyclerAdapter<Games, ViewHolder> firebaseRecyclerAdapter =
                new FirebaseRecyclerAdapter<Games, ViewHolder>(
                        Games.class,
                        R.layout.cardview_list_item,
                        ViewHolder.class,
                        mRef
                ) {
                    @Override
                    protected void populateViewHolder(ViewHolder viewHolder, Games games, int i) {

                        viewHolder.setDetails(getApplicationContext(), games.getTitle(), games.getImage(),
                                games.getSize(),games.getDownload(),games.getDiscription());
                    }

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

                        ViewHolder viewHolder = super.onCreateViewHolder(parent,viewType);

                        viewHolder.setOnClickListener(new ViewHolder.ClickListener() {
                            @Override
                            public void onItemClick(View view, int position) {

                                Toast.makeText(MainActivity.this,"You Clicked on this Game",Toast.LENGTH_SHORT).show();

                            }

                            @Override
                            public void onItemLongClick(View view, int position) {
                                Toast.makeText(MainActivity.this,"You Long Clicked on this Game",Toast.LENGTH_SHORT).show();

                            }
                        });

                        return super.onCreateViewHolder(parent, viewType);
                    }
                };


        recyclerView.setAdapter(firebaseRecyclerAdapter);


    }

并检查模型类

    package com.example.my.app;

public class Games {

    String title;
    String size;
    String image;
    String download;
    String discription;

    public Games() {
    }

    public String getTitle() {
        return title;
    }

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

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getDownload() {
        return download;
    }

    public void setDownload(String download) {
        this.download = download;
    }

    public String getDiscription() {
        return discription;
    }

    public void setDiscription(String discription) {
        this.discription = discription;
    }
}

共有1个答案

凤昊东
2023-03-14
ViewHolder viewHolder = super.onCreateViewHolder(parent,viewType);

用这个

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.your_xml_layout, parent, false);
return new ViewHolder(view);

@override onbindviewholder方法

@Override
protected void onBindViewHolder(ViewHolder holder, final int position, Model model) {
    holder.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              // your click logic
          }
     });
}

查询此链接

 类似资料:
  • 然后,在每个@test方法中,我调用activitytestrule.launchactivity(Intent.action_main)),然后进行单击。我发现,只有第一次点击不起作用,但第二次和第三次点击起作用。那么,这种行为的原因又能是什么呢?

  • 如果用户点击一个,我希望它做一些事情,也为两个和三个。我如何实现这一点?

  • 我正面临一个奇怪的错误,其中recyclerview只显示了一个项目。下面是我的recyclerview适配器的代码: 我已经在其他应用程序中使用了这段代码,它的工作非常完美。我已经检查了聊天数据,这也是完美的。 这里是指向git repo布局文件的链接:布局文件

  • 我有音频播放器布局作为一个项目在回收器视图。我想播放图标改变为暂停图标在图标点击。日志显示图标已经设置,但图标在回收器视图中的外观没有改变。 我的代码: 我实际上有一个线性布局音频项目。我正在设置onClickListener()no一个ImageButton来将其背景从play更改为pause。 额外信息: 我观察到第一次点击图标并没有什么作用。 第二次点击它会将它改变为暂停图标,但它会迅速返回

  • 如何在回收者视图中收听触摸事件?我已经为视图持有者实现了click listener,但它可以在整个视图上工作。我在每个视图支架中都有一个图像视图,当用户单击图像视图时,我需要执行一个操作。 当前视图持有者代码。