My main activity (MainActivity) contains Recycler view with list of images and text in cardview. I am trying to put separate onCLick listener on each cardview so that when any card view is clicked, the next activity starts(MainActivity2). the cardview shows the channels image and names and when clicked on any channel, the user moves to the list of videos from that channel. MAinActivity code for recycler view
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);
}
It loads as the interface ItemClickListener is defined as
package com.currentmedia.punjabinews;
import android.view.View;
public interface ItemClickListener {
void onItemClickListener(View v, int position);
}
the adapter and viewholder class is defined as;
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;
}
}
}
the MAinActivity 2 is a Recycler View List of videos of channel using youtube DataAPI; the excerpet of code is;
public class MainActivity2 extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<VideoDetails> videoDetailsoArrayList;
Intent intent=getIntent();
this is the screen shot of MainActivity2 that I want to run on click.
But when I click on the icon the app crashes. the log error is shown as;
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)
I cannot understand why it is giving null object reference when I added intent in MainActivity 2 as well