微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过单击项目开始另一个活动recyclerview列表

如何解决通过单击项目开始另一个活动recyclerview列表

我的主要活动(MainActivity)包含“回收站”视图,以及卡片视图中的图像和文本列表。我试图在每个卡片视图上放置单独的onCLick侦听器,以便单击任何卡片视图时,下一个活动开始(MainActivity2)。卡片视图会显示频道图像和名称,并且在任何频道上单击时,用户都将从该频道移至视频列表。 用于回收者视图的MAinActivity代码


private String[] channelnames={"PTC Punjabi","Chakde TV","T-Series Punjabi","9X Tashan","Zee Punjabi" };
    private int[] channelimages={R.drawable.ptcpunjabi,R.drawable.chakde,R.drawable.tseries,R.drawable.ninex,R.drawable.zeepunjabi};
    private List<channel> channelList=new ArrayList<>();

 thirdrecyclerView=findViewById(R.id.third_recycler_view);
        thirdrecyclerView.setHasFixedSize(true);
        channelList=new ArrayList<>();
        linearlayoutmanager thirdlinearlayoutmanager = new linearlayoutmanager(this,linearlayoutmanager.HORIZONTAL,false);
        thirdrecyclerView.setLayoutManager(thirdlinearlayoutmanager);
        for (int i=0;i < channelnames.length;i++){
            channel channel=new channel(channelnames[i],channelimages[i]);
            channelList.add(channel);
        }

加载为

Horizontal recycler view

接口 ItemClickListener 定义为

package com.currentmedia.punjabinews;

        import android.view.View;

public interface ItemClickListener {
    void onItemClickListener(View v,int position);
}

适配器和观看者类定义为;

public class channeladpater extends RecyclerView.Adapter<channeladpater.Channelviewholder> {
    private List<channel> channelList;
    Context ctx;

    public channeladpater(List<channel> channelList) {
        this.channelList = channelList;
        this.ctx=ctx;
    }

    @NonNull
    @Override
    public Channelviewholder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view,parent,false);
        return new Channelviewholder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Channelviewholder holder,int position)
    {
        channel channel=channelList.get(position);
        holder.channelname.setText(channel.getChannelname());
        holder.channelimage.setimageResource(channel.getChannelimage());

        holder.setItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClickListener(View v,int position) {
                Intent intent=new Intent(ctx,MainActivity2.class);
                ctx.startActivity(intent);

            }
        });

    }

    @Override
    public int getItemCount() {
        return channelList.size();
    }

    public static class Channelviewholder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public TextView channelname;
        public CircleImageView channelimage;
        ItemClickListener itemClickListener;

        Channelviewholder(@NonNull View itemView) {
            super(itemView);
           this.channelname=itemView.findViewById(R.id.profile_name);
            this.channelimage=itemView.findViewById(R.id.profile_image);
            itemView.setonClickListener(this);
        }

        @Override
        public void onClick(View v) {

            this.itemClickListener.onItemClickListener(v,getLayoutPosition());


        }
        public void setItemClickListener(ItemClickListener ic){
            this.itemClickListener=ic;
        }
    }
}

MAinActivity 2是使用youtube DataAPI的频道视频的回收者视图列表;代码摘要是;

public class MainActivity2 extends AppCompatActivity {

        RecyclerView recyclerView;
        ArrayList<VideoDetails> videoDetailsoArrayList;
       
        Intent intent=getIntent();

这是我要单击运行的MainActivity2的屏幕截图。

MainActivity2

但是,当我单击图标时,应用程序崩溃了。日志错误显示为;

2020-09-18 00:34:33.348 30473-30514/com.currentmedia.punjabinews E/cr_VariationsUtils: Failed reading seed file "/data/user/0/com.currentmedia.punjabinews/app_webview/variations_seed": /data/user/0/com.currentmedia.punjabinews/app_webview/variations_seed (No such file or directory)
2020-09-18 00:34:33.697 30473-30553/com.currentmedia.punjabinews E/eglCodecCommon: glutilsParamSize: unkNow param 0x000082da
2020-09-18 00:34:33.698 30473-30553/com.currentmedia.punjabinews E/eglCodecCommon: glutilsParamSize: unkNow param 0x000082da
2020-09-18 00:34:34.255 30473-30593/com.currentmedia.punjabinews E/eglCodecCommon: glutilsParamSize: unkNow param 0x000082da
2020-09-18 00:34:34.256 30473-30593/com.currentmedia.punjabinews E/eglCodecCommon: glutilsParamSize: unkNow param 0x000082da
2020-09-18 00:34:34.923 30473-30473/com.currentmedia.punjabinews E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-18 00:34:35.022 30473-30473/com.currentmedia.punjabinews E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-18 00:34:35.062 30473-30473/com.currentmedia.punjabinews E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-18 00:34:35.121 30473-30473/com.currentmedia.punjabinews E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-18 00:34:35.170 30473-30473/com.currentmedia.punjabinews E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-18 00:34:35.194 30473-30473/com.currentmedia.punjabinews E/ThemeUtils: View class androidx.appcompat.widget.AppCompatimageView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-18 00:34:35.197 30473-30473/com.currentmedia.punjabinews E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-18 00:34:35.213 30473-30473/com.currentmedia.punjabinews E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-18 00:34:35.215 30473-30473/com.currentmedia.punjabinews E/ThemeUtils: View class androidx.appcompat.widget.AppCompatimageView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-18 00:34:35.224 30473-30473/com.currentmedia.punjabinews E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-18 00:34:35.266 30473-30473/com.currentmedia.punjabinews E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-18 00:34:35.269 30473-30473/com.currentmedia.punjabinews E/ThemeUtils: View class androidx.appcompat.widget.AppCompatimageView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-18 00:34:35.270 30473-30473/com.currentmedia.punjabinews E/ThemeUtils: View class androidx.appcompat.widget.AppCompatimageView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-18 00:34:35.273 30473-30473/com.currentmedia.punjabinews E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-18 00:34:35.278 30473-30473/com.currentmedia.punjabinews E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-18 00:34:35.289 30473-30473/com.currentmedia.punjabinews E/ThemeUtils: View class androidx.appcompat.widget.AppCompatimageView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-18 00:34:35.293 30473-30473/com.currentmedia.punjabinews E/ThemeUtils: View class androidx.appcompat.widget.AppCompatimageView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-18 00:34:35.297 30473-30473/com.currentmedia.punjabinews E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-18 00:34:35.301 30473-30473/com.currentmedia.punjabinews E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-18 00:34:35.306 30473-30473/com.currentmedia.punjabinews E/ThemeUtils: View class androidx.appcompat.widget.AppCompatimageView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-18 00:34:35.308 30473-30473/com.currentmedia.punjabinews E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-18 00:34:35.313 30473-30473/com.currentmedia.punjabinews E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-18 00:34:35.470 30473-30596/com.currentmedia.punjabinews E/YouTubeAndroidplayerAPI: Embed config is not supported in RemoteEmbeddedplayer.
2020-09-18 00:34:37.997 30473-30473/com.currentmedia.punjabinews E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.currentmedia.punjabinews,PID: 30473
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
        at android.content.ComponentName.<init>(ComponentName.java:130)
        at android.content.Intent.<init>(Intent.java:5780)
        at com.currentmedia.punjabinews.channeladpater$1.onItemClickListener(channeladpater.java:47)
        at com.currentmedia.punjabinews.channeladpater$Channelviewholder.onClick(channeladpater.java:75)
        at android.view.View.performClick(View.java:6294)
        at android.view.View$PerformClick.run(View.java:24770)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
2020-09-18 00:34:49.144 30473-30593/com.currentmedia.punjabinews E/Surface: queueBuffer: error queuing buffer to SurfaceTexture,-19
2020-09-18 00:34:49.145 30473-30593/com.currentmedia.punjabinews E/EGL_emulation: tid 30593: swapBuffers(552): error 0x300d (EGL_BAD_SURFACE)

当我在MainActivity 2中添加意图时,我也无法理解为什么给出空对象引用

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。