RecyclerView未显示数据:Volley解析JSON后数据展示异常排查
Hey there, let's work through your problems step by step—you've got a couple of key issues causing your RecyclerView not to populate and that frustrating NullPointerException.
1. Root Cause of the NullPointerException
The crash happens because mContext in your NewsAdapter is null. You're trying to use it for Glide and starting an Intent, but you never initialized it properly in the adapter's constructor.
Fix the NewsAdapter Constructor & Context Setup
Update your NewsAdapter to accept a Context in its constructor and store it:
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> { private Context mContext; private List<News> newsList; // Updated constructor with Context public NewsAdapter(Context context, List<News> newsList) { this.mContext = context; this.newsList = newsList; } // Rest of your adapter code (onCreateViewHolder, ViewHolder class)... @Override public void onBindViewHolder(@NonNull NewsAdapter.ViewHolder holder, int position) { final News currentNews = newsList.get(position); holder.tvTitle.setText(currentNews.getTitle()); // Use the initialized mContext directly (no need for getApplicationContext() here) Glide.with(mContext).load(currentNews.getImage()).into(holder.ivArticleImage); holder.linearLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Uri newsUri = Uri.parse(currentNews.getNewsLink()); Intent toWebSiteIntent = new Intent(Intent.ACTION_VIEW, newsUri); mContext.startActivity(toWebSiteIntent); } }); } }
Then update the adapter initialization in HomeActivity to pass the context:
// Replace your existing adapter line with this adapter = new NewsAdapter(this, newsList);
2. Why Your RecyclerView Isn't Showing Data
In your getData() method, you're creating a local newsList instead of using the global one you initialized in onCreate(). This means you're parsing data into a list that never gets attached to your adapter.
Fix the Data Parsing in getData()
Modify the getData() method to use your global newsList and add parsed items to it:
private void getData() { final ProgressDialog progressDialog = new ProgressDialog(this); progressDialog.setMessage("Loading..."); progressDialog.show(); JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { // Clear old data first to avoid duplicates on reloading newsList.clear(); for(int i = 0; i < response.length(); i++){ try{ JSONObject jsonObject = response.getJSONObject(i); News article = new News(); article.setTitle(jsonObject.getString("title")); article.setImage(jsonObject.getString("image")); // Add the parsed article to the GLOBAL newsList newsList.add(article); }catch (JSONException e){ e.printStackTrace(); progressDialog.dismiss(); } } // Notify adapter that data has changed adapter.notifyDataSetChanged(); progressDialog.dismiss(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("Volley", error.toString()); progressDialog.dismiss(); } }); RequestQueue requestQueue = Volley.newRequestQueue(this); requestQueue.add(jsonArrayRequest); }
3. Quick Additional Checks
- Double-check that your
Newsclass has valid getter methods (getTitle(),getImage(),getNewsLink()) that correctly return the data you set. - Ensure your JSON response actually contains the keys
"title","image", and"newsLink"—a typo here would silently fail to populate data.
After making these changes, your RecyclerView should display the parsed data, and the NullPointerException should be resolved.
内容的提问来源于stack exchange,提问作者esQmo_

