我的主要活动(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);
}
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 的频道视频的 Recycler View List;代码摘录是;
public class MainActivity2 extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<VideoDetails> videoDetailsoArrayList;
Intent intent=getIntent();
这是我想在点击时运行的 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 中添加意图时,我无法理解为什么它会给出空对象引用