使用Bundle在同一Activity的Fragment间传值失败,程序崩溃求助
Hey there, let’s troubleshoot this Fragment data passing crash together! Since you’re new to Fragments, it’s super common to hit snags with Bundle transfers—let’s break down your issue and fix it step by step.
You mentioned your app crashes immediately with no output when trying to send data between two Fragments in the same Activity using a Bundle. Here’s the partial code snippet you shared for your FirstFragment:
//first fragment public class FirstFragment extends Fragment implements View.OnClickListener{ public FirstFragment() { } Button btnSend; EditText etTextContainer; Bundle b; SecondFragment fragB; View v; FragmentTransaction fragmentTransaction; Fragment fragment; SecondFragment mfragment; String etex; @Override // Rest of your code is truncated... }
Let’s start with the most likely causes of your crash—empty crashes usually point to null pointer exceptions or incorrect Fragment setup:
1. Uninitialized Fragment Instances
Looking at your code, you’ve declared two SecondFragment variables (fragB and mfragment) but haven’t shown where you initialize them. Using an uninitialized (null) Fragment instance will trigger a crash immediately.
Fix: Always create a new instance of your target Fragment right before passing data:
SecondFragment secondFragment = new SecondFragment();
2. Incorrect Bundle Passing
You might be trying to assign the Bundle directly to a Fragment instead of using the official setArguments() method—the only safe way to pass data to a Fragment during initialization.
Fix: Here’s the correct flow to pack and send data:
// Inside your onClick method for btnSend String etex = etTextContainer.getText().toString().trim(); // Create Bundle and add your data Bundle bundle = new Bundle(); bundle.putString("USER_INPUT", etex); // Use a unique key to retrieve later // Attach Bundle to the new Fragment instance secondFragment.setArguments(bundle);
3. Broken Fragment Transaction
If your FragmentTransaction isn’t properly executed (e.g., missing commit(), using the wrong container ID), this will cause crashes or unexpected behavior.
Fix: Execute the transaction correctly from your FirstFragment:
// Get the FragmentManager from your hosting Activity FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction(); // Replace the container with your SecondFragment (use your actual container ID) transaction.replace(R.id.fragment_container, secondFragment); // Optional: Add to back stack so users can navigate back transaction.addToBackStack(null); // Commit the transaction to apply changes transaction.commit();
4. Retrieving Data in SecondFragment
Don’t forget to properly fetch the data in your SecondFragment—incorrect Bundle access will cause another crash, and skipping this means you won’t see your data at all.
Example code for SecondFragment:
public class SecondFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_second, container, false); TextView displayText = view.findViewById(R.id.tv_display); // Get the Bundle passed from FirstFragment Bundle bundle = getArguments(); if (bundle != null) { String receivedText = bundle.getString("USER_INPUT"); displayText.setText(receivedText); } return view; } }
- Always initialize Fragments before using them—never use a null Fragment instance.
- Make sure your Activity layout has a valid container (like a
FrameLayout) with the ID you use inreplace(). - Avoid calling
getActivity()inonCreate()—wait untilonViewCreated()to ensure the Activity is fully attached. - Double-check that the key you use to put data in the Bundle matches the key you use to retrieve it (it’s case-sensitive!).
内容的提问来源于stack exchange,提问作者jannat suha

