点击Fragment内RecyclerView项跳转Activity时App崩溃
Hey there! Let's break down why your app is crashing when tapping a RecyclerView item to launch an Activity, and fix it up. Based on your code snippet and common Android pitfalls, here are the most likely culprits and their solutions:
1. Invalid or Missing Context (Most Common)
A super frequent issue here is using the wrong Context to start the Activity. If you're using getApplicationContext() (which isn't tied to an Activity lifecycle) or a Context that's already been destroyed (like a detached Fragment's Context), the app will crash.
Fix:
- Pass a valid Activity Context to your Adapter via its constructor, or safely use the
Contextfrom the item view:// In your Adapter constructor private Context mContext; private List<News> mNewsList; public NewsAdapter(Context context, List<News> newsList) { mContext = context; mNewsList = newsList; } // In onBindViewHolder (or onCreateViewHolder, see tip below) holder.itemView.setOnClickListener(v -> { if (currentItem == null) return; Intent intent = new Intent(mContext, YourTargetActivity.class); // Pass data to the Activity intent.putExtra("NEWS_TITLE", currentItem.getTitle()); // Ensure we're using a valid Activity Context if (mContext instanceof Activity) { mContext.startActivity(intent); } else { // Fallback only if necessary (not recommended for app-internal Activities) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mContext.startActivity(intent); } });
2. Target Activity Not Registered in Manifest
If your app can't find the Activity you're trying to launch, it'll crash immediately. This happens when you forget to declare the Activity in AndroidManifest.xml.
Fix:
Open your AndroidManifest.xml and add the Activity declaration inside the <application> tag:
<activity android:name=".path.to.YourTargetActivity" /> <!-- Replace .path.to with your actual package/activity path -->
Also double-check that the class name in your Intent matches exactly (typos are easy to miss!).
3. Null Pointer Exception from Missing Data
Even though you fetch currentItem in onBindViewHolder, if your mNewsList gets modified after binding (like during a background refresh) or the position becomes invalid, accessing currentItem.getTitle() can trigger a crash.
Fix:
- Add null checks for
currentItembefore using it:holder.itemView.setOnClickListener(v -> { if (currentItem == null) { return; // Exit early to avoid crash } // Proceed with creating Intent and launching Activity }); - If your list updates dynamically, always use
notifyDataSetChanged()(or more efficient methods likenotifyItemInserted()) on the main thread to keep the RecyclerView in sync.
4. Inefficient or Broken Click Binding
Binding click listeners inside onBindViewHolder works, but it can lead to redundant bindings as items scroll. Worse, if you're using an outdated position value, you might access the wrong list item.
Fix:
Bind the click listener in onCreateViewHolder instead, and use holder.getAdapterPosition() to get the current valid position:
@Override public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.your_item_layout, parent, false); CardViewHolder holder = new CardViewHolder(itemView); // Bind click listener once when creating the ViewHolder holder.itemView.setOnClickListener(v -> { int position = holder.getAdapterPosition(); if (position == RecyclerView.NO_POSITION) return; // Skip invalid positions News currentItem = mNewsList.get(position); if (currentItem == null) return; Intent intent = new Intent(parent.getContext(), YourTargetActivity.class); intent.putExtra("NEWS_DATA", currentItem); // If News implements Parcelable parent.getContext().startActivity(intent); }); return holder; }
5. Unhandled Permissions (Rare but Possible)
If your target Activity requires certain runtime permissions (e.g., accessing external storage for a news attachment), and you haven't requested them, the app might crash when launching the Activity.
Fix:
- Check if your Activity needs any permissions, then declare them in
AndroidManifest.xmland request them at runtime (for Android 6.0+).
内容的提问来源于stack exchange,提问作者mazen el zoor

