Android Studio中使用Bundle传递ArrayList时出现空指针异常问题求助
咱们一步一步拆解你遇到的问题,这个空指针异常的原因其实有好几处,我给你捋得明明白白:
核心问题分析
第二个Fragment里新建了空Bundle,根本没拿到第一个Fragment传递的内容
看你第二个Fragment的代码:Bundle b=new Bundle ();,这完全是创建了一个全新的空Bundle,里面根本没有你在第一个Fragment里放的ListPlatforms数据,所以b.getSerializable("ListPlatforms")自然返回null,遍历null集合的时候就触发了空指针异常。第一个Fragment里存数据到Bundle的时机不对
doGetRequest是异步网络请求,你在请求还没完成、data还没被填充任何数据的时候,就执行了b.putSerializable("ListPlatforms", (Serializable) data);,这时候data要么是空的,要么还没加载到数据,就算能传递过去也没用。潜在隐患:Platform类是否实现了Serializable接口?
你强制把data转成Serializable存到Bundle里,但如果你的Platform类没有实现Serializable接口,这里会出现运行时异常,就算没报错,也没法正确序列化传递数据。
解决方案
1. 修正Bundle的传递逻辑
第一个Fragment要把包含数据的Bundle传递给第二个Fragment,而不是自己存着;第二个Fragment要获取传递过来的Bundle,而不是新建空的。
比如在第一个Fragment里,等数据加载完成后,把Bundle设置给第二个Fragment:
@Override public void onCompleted(String response) { try { JSONArray jsonArray = new JSONArray(response); for(int i=0;i<jsonArray.length();i++){ JSONObject object= jsonArray.optJSONObject(i); if(object!=null){ data.add(Platform.parseJSON(object)); } } adapter.notifyDataSetChanged(); // 数据加载完成后,再创建Bundle并传递给第二个Fragment Bundle bundle = new Bundle(); bundle.putSerializable("ListPlatforms", data); // 示例:创建第二个Fragment实例并传递Bundle SecondMapFragment mapFragment = new SecondMapFragment(); mapFragment.setArguments(bundle); // 执行Fragment跳转(根据你的页面结构调整) getParentFragmentManager() .beginTransaction() .replace(R.id.fragment_container, mapFragment) .commit(); }catch(JSONException e){ e.printStackTrace(); } }
2. 第二个Fragment里正确获取Bundle并判空
不要新建Bundle,用getArguments()获取传递过来的内容,并且一定要先判空,避免空指针:
@Override public void onLocationChanged(@NonNull Location location) { if (googleMap != null) { // 你的位置标记代码保持不变 if (myMarker == null) { MarkerOptions options = new MarkerOptions(); options.title("My location"); options.position(new LatLng(location.getLatitude(), location.getLongitude())); myMarker = googleMap.addMarker(options); googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 10f)); } else { myMarker.setPosition(new LatLng(location.getLatitude(), location.getLongitude())); } } // 获取传递过来的Bundle,而不是新建 Bundle bundle = getArguments(); if (bundle != null) { ArrayList<Platform> platforms = (ArrayList<Platform>) bundle.getSerializable("ListPlatforms"); // 先判断集合不为空再遍历 if (platforms != null && !platforms.isEmpty()) { for(Platform platform : platforms) { double latitudine = platform.getClatitudine__wgs84__(); double longitudine = platform.getClongitudine__wgs_84__(); double distance = getDistanceinKm(latitudine, longitudine, location.getLatitude(), location.getLongitude()); if (distance >= 0.0) { MarkerOptions options = new MarkerOptions(); options.title(platform.getCdenominazione__()); options.position(new LatLng(latitudine, longitudine)); // 这里注意不要覆盖myMarker,建议用另一个变量存储平台标记 googleMap.addMarker(options); } } } } }
3. 确保Platform类实现Serializable接口
打开你的Platform类,让它实现Serializable接口,这样才能被Bundle正确序列化传递:
import java.io.Serializable; public class Platform implements Serializable { // 你的类成员变量、getter/setter、parseJSON方法等 }
额外提示
- 如果你用的是Jetpack Navigation组件,建议用Safe Args来传递数据,比直接用Bundle更安全,能避免键名写错、类型转换错误的问题。
- 遍历集合前一定要判空,这是避免空指针的好习惯,哪怕你确定数据一定存在,也最好加上判断,防止边界情况。
内容的提问来源于stack exchange,提问作者Martak39




