onclickitem不工作,错误为未连接适配器,跳过布局

wwwo4jvm  于 2021-07-04  发布在  Java
关注(0)|答案(1)|浏览(325)

这个问题在这里已经有答案了

什么是nullpointerexception,如何修复它(12个答案)
4个月前关门了。
我正在使用2个活动(主要和视频播放活动)。它显示一个回收视图,当点击任何项目的应用程序停止工作。
我试图为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)

我的adatper课是这样的

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);

                }

        }
    }

我的主要活动如下

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,无适配器连接,跳过布局,尝试在getactivity()上调用虚拟方法'java.lang.string android.content.context.getpackagename(),我还应用了适配器构造函数ctx=ctx中的npe声明方法

kdfy810k

kdfy810k1#

您的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,因为适配器类中已经存在上下文ctx引用。

相关问题