Fragment间传值报错:Bundle.getInt()空对象引用问题求助
看起来你遇到的空指针问题根源很明确:发送方的代码逻辑顺序错误,导致Profile_Fragment根本没有收到你封装的Bundle参数。我们一步步拆解问题并解决:
为什么会出现空指针?
看你发送方的核心代码:
startActivity(new Intent(getActivity(), ActivityListEvents.class)); Bundle bundle = new Bundle(); bundle.putInt("id", id); Profile_Fragment mFragment_B = new Profile_Fragment(); mFragment_B.setArguments(bundle);
你先调用了startActivity启动ActivityListEvents,这会让当前页面被新的Activity完全覆盖,后续创建Fragment和设置Bundle的代码其实没有任何实际作用——因为这个mFragment_B并没有被添加到任何页面中。而你在ActivityListEvents里加载的Profile_Fragment是全新的实例,自然没有携带任何参数,所以getArguments()返回null,调用bundle.getInt("id")就会触发空指针异常。
针对两种场景的解决方案
首先要明确你的需求:是跳转到ActivityListEvents后在该Activity中显示Profile_Fragment并传递ID?还是在当前页面直接切换到Profile_Fragment?
场景1:跳转到ActivityListEvents后显示Profile_Fragment
我们需要先把ID通过Intent传递给目标Activity,再在Activity中创建Fragment并传递参数:
步骤1:修改发送方代码(传递ID到Activity)
public void onResponse(Call<String> call, Response<String> response) { if (response.isSuccessful() && response.body() != null) { String jsonresponse = response.body().toString(); try { JSONObject jsonObject = new JSONObject(jsonresponse); if (jsonObject.getString("status").equals("success")) { JSONObject jsonData = jsonObject.getJSONObject("data"); Integer id = jsonData.getInt("id"); // 创建Intent并携带ID Intent intent = new Intent(getActivity(), ActivityListEvents.class); intent.putExtra("user_id", id); // 用一个明确的key传递ID startActivity(intent); } } catch (JSONException e) { e.printStackTrace(); } } }
步骤2:在ActivityListEvents中传递ID到Fragment
假设你的ActivityListEvents布局里有一个Fragment容器(比如FrameLayout,ID为fragment_container),在Activity的onCreate方法中处理:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_events); // 从Intent中获取ID,-1是默认值(表示未获取到) int userId = getIntent().getIntExtra("user_id", -1); if (userId == -1) { Log.e("ActivityListEvents", "Failed to get user ID"); finish(); // 如果没拿到ID,可以选择关闭页面 return; } // 创建Profile_Fragment并设置参数 Profile_Fragment profileFragment = new Profile_Fragment(); Bundle bundle = new Bundle(); bundle.putInt("id", userId); profileFragment.setArguments(bundle); // 将Fragment添加到容器中 getSupportFragmentManager().beginTransaction() .replace(R.id.fragment_container, profileFragment) .commit(); }
场景2:在当前页面直接切换到Profile_Fragment
如果不需要跳转到新Activity,直接在当前页面替换Fragment,那么修改发送方代码,去掉startActivity,改为执行Fragment事务:
public void onResponse(Call<String> call, Response<String> response) { if (response.isSuccessful() && response.body() != null) { String jsonresponse = response.body().toString(); try { JSONObject jsonObject = new JSONObject(jsonresponse); if (jsonObject.getString("status").equals("success")) { JSONObject jsonData = jsonObject.getJSONObject("data"); Integer id = jsonData.getInt("id"); // 创建Fragment并设置参数 Profile_Fragment mFragment_B = new Profile_Fragment(); Bundle bundle = new Bundle(); bundle.putInt("id", id); mFragment_B.setArguments(bundle); // 替换当前页面的Fragment容器(需要替换成你布局中的容器ID) getActivity().getSupportFragmentManager().beginTransaction() .replace(R.id.current_fragment_container, mFragment_B) .addToBackStack(null) // 可选:添加到返回栈,让用户可以返回上一个Fragment .commit(); } } catch (JSONException e) { e.printStackTrace(); } } }
接收方代码优化(增加空安全判断)
无论哪种场景,接收方都应该增加空判断,避免因为参数传递失败导致的异常:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view= inflater.inflate(R.layout.profile_fragment, container, false); initViews(); setListeners(); Bundle bundle = getArguments(); if (bundle != null) { // 第二个参数-1是默认值,当key不存在时返回 int id = bundle.getInt("id", -1); if (id != -1) { Log.i("ID REC",String.valueOf(id)); // 在这里安全调用Retrofit的updateUsers接口 } else { Log.e("Profile_Fragment", "Received invalid user ID"); } } else { Log.e("Profile_Fragment", "No arguments passed to Fragment"); } return view; }
这样修改后,你的ID就能正确传递到Profile_Fragment,空指针问题也会解决,之后就可以正常调用updateUsers接口了。
内容的提问来源于stack exchange,提问作者DjouBN

