-1

In my apllication i use bottom navigation view. In the third fragment must be map. And i don`t know how to send data in this fragment.

Method in ActivityBotom.class

     @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment fragment = null;
        switch (item.getItemId()) {
            case R.id.nav_home:
                fragment = new FragmentMarker();
                break;
            case R.id.nav_bookmark:
                fragment = new FragmentBookmark();
                break;
            case R.id.nav_blog:
                fragment = new FragmentMap();
                break;
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, fragment).commit();
        return true;

    }

And this is my FragmentMap.class

public class FragmentMap extends Fragment  {

      @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_map, container, false);
        mMapView = getArguments().getParcelable(EXTRA_MARKER_LIST);
        mMapView =  rootView.findViewById(R.id.mapView);
        mMapView.onCreate(savedInstanceState);
        mMapView.onResume(); 
        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }

        mMapView.getMapAsync(new OnMapReadyCallback() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onMapReady(GoogleMap mMap) {
                googleMap = mMap;
              ...
              Log.d("debug", "problem"); 
                }

                List<Marker> markerList = getActivity().getIntent().getParcelableArrayListExtra(EXTRA_MARKER_LIST);
                for (int i = 0; i < markerList.size(); i++) {
                    Marker marker = markerList.get(i);
                    Location location = marker.getLocation();
                    clusterManager.setItems(markerList);
                }
            }

        });

And i keep getting this error java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' on a null object reference

I think in case for map myst be Bundle. But i don`t know how to implement it. I hope someone will help me. Thanks

MarinMax
  • 1
  • 2
  • The error start there `getArguments().getParcelable(EXTRA_MARKER_LIST)` – Crammeur Aug 04 '18 at 22:20
  • Check this https://stackoverflow.com/questions/10107442/android-how-to-pass-parcelable-object-to-intent-and-use-getparcelable-method-of – Crammeur Aug 04 '18 at 22:25
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Crammeur Aug 04 '18 at 22:27

1 Answers1

0

You can send the data using arguments

Bundle bundle = new Bundle();
bundle.putSerializable(EXTRA_MARKER_LIST, ________);
fragment = new FragmentMap();
fragment.setArguments(bundle);

This will fix the issue

Viswanath Kumar Sandu
  • 2,230
  • 2
  • 17
  • 34