onClickItem无法工作,出现错误未连接适配器,正在跳过布局



我正在使用两个活动(Main和VideoPlayActivity(。它显示了一个回收器视图,当点击任何项目时,应用程序都会停止工作。

我正试图为onCLickItem创建一个运行新活动的意图,我收到了这个错误

2020-09-06 09:48:56.601 15956-15956/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2020-09-06 09:48:58.827 15956-15956/com.currentmedia.channel E/RecyclerView: No adapter attached; skipping layout
2020-09-06 09:49:10.757 15956-15956/com.currentmedia.channel E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.currentmedia.channel, PID: 15956
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.currentmedia.channel.VideoDetails.getVideoId()' on a null object reference
at com.currentmedia.channel.adapter$1.onClick(adapter.java:54)
at android.view.View.performClick(View.java:6935)
at android.view.View$PerformClick.run(View.java:26214)
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:7025)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

我的adapper类是这样的

public class adapter extends RecyclerView.Adapter<adapter.ViewHolder> {
LayoutInflater inflater;
ArrayList<VideoDetails> videoDetailsArrayList;
Context ctx;
VideoDetails videoDetails;
RecyclerView recyclerView;

//Constructor for adaptor
public adapter(Context ctx, ArrayList<VideoDetails> videoDetailsArrayList)
{
this.videoDetailsArrayList=videoDetailsArrayList;
//this.videoDetails=videoDetails;
this.ctx=ctx;
this.inflater=LayoutInflater.from(ctx);
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.custom_layout,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textView.setText(videoDetailsArrayList.get(position).getTitle());
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(ctx, VideoPlayActivity.class);
i.putExtra("videoId", videoDetails.getVideoId());
ctx.startActivity(i);
}
});
final VideoDetails videoDetails=(VideoDetails)
this.videoDetailsArrayList.get(position);
// final VideoDetails videoDetails=(VideoDetails) this.videoDetailsArrayList.get(position);
Picasso.get().load(videoDetails.getUrl()).into(holder.imageView);

}
@Override
public int getItemCount()                                                                                                                           {
return videoDetailsArrayList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView textView;
ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
textView=itemView.findViewById(R.id.mytitle);
imageView=itemView.findViewById(R.id.imageView);

}
}
}

我的MAinActivity如下

public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<VideoDetails> videoDetailsoArrayList;
String API_Key = "";
String url="https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCVMWWQ985A_-SESZUy_SsVQ&maxResults=50&key=";
adapter adapter;
Context ctx;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=findViewById(R.id.listView);
videoDetailsoArrayList= new ArrayList<>();
adapter=new adapter(MainActivity.this,videoDetailsoArrayList);
displayVideos();
}

private void displayVideos ()
{
RequestQueue requestQueue= Volley.newRequestQueue(this);
StringRequest stringRequest=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {

@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("items");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
if (jsonObject1.has("id")){
JSONObject jsonVideoId=jsonObject1.getJSONObject("id");
if (jsonVideoId.has("kind")){
if(jsonVideoId.getString("kind").equals("youtube#video")){
JSONObject jsonObjectSnippet = jsonObject1.getJSONObject("snippet");
JSONObject jsonObjectDefault=jsonObjectSnippet.getJSONObject("thumbnails").getJSONObject("medium");
String video_id=jsonVideoId.getString("videoId");
VideoDetails vd=new VideoDetails();
vd.setVideoId(video_id);
vd.setTitle(jsonObjectSnippet.getString("title"));
vd.setDescription(jsonObjectSnippet.getString("description"));
vd.setUrl(jsonObjectDefault.getString("url"));
videoDetailsoArrayList.add(vd);
}
//  recyclerView.setAdapter(adapter);
// adapter.notifyDataSetChanged();
}
}
}
}catch (JSONException e) {
e.printStackTrace();
}
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
adapter= new adapter(getApplicationContext(),videoDetailsoArrayList);
recyclerView.setAdapter(adapter);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),error.getMessage(), LENGTH_LONG).show();
}
});
requestQueue.add(stringRequest);
}
}

我已经尝试过堆叠器解决方案没有连接适配器;跳过布局:RecyclerView,无适配器连接,跳过布局,尝试调用虚拟方法';java.lang.String android.content.Context.getPackageName((on getActivity((,我还在适配器构造函数ctx=ctx-中应用了NPE声明方法

您的Viewholder.ctx在该对象中没有上下文引用,因此出现错误,我们没有在Viewholder类中设置onClickListener,请查看以下内容。

public class adapter extends RecyclerView.Adapter<adapter.ViewHolder> {
LayoutInflater inflater;
ArrayList<VideoDetails> videoDetailsArrayList;
Context ctx;
VideoDetails videoDetails;
RecyclerView recyclerView;

//Constructor for adaptor
public adapter(Context ctx, ArrayList<VideoDetails> videoDetailsArrayList)
{
this.inflater=LayoutInflater.from(ctx);
this.videoDetailsArrayList=videoDetailsArrayList;
this.videoDetails=videoDetails;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.custom_layout,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textView.setText(videoDetailsArrayList.get(position).getTitle());
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(ctx, VideoPlayActivity.class);
i.putExtra("videoId", videoDetails.getVideoId());
ctx.startActivity(i);
}
});
final VideoDetails videoDetails=(VideoDetails) 
this.videoDetailsArrayList.get(position);
Picasso.get().load(videoDetails.getUrl()).into(holder.imageView);
}
@Override
public int getItemCount() {
return videoDetailsArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView textView;
ImageView imageView;
Context ctx;
public ViewHolder(View itemView) {
super(itemView);
textView=itemView.findViewById(R.id.mytitle);
imageView=itemView.findViewById(R.id.imageView);
}
}
}

这样,您就不需要将上下文传递给ViewHolder,因为context ctx引用已经存在于Adapter类中。

最新更新