MapFragment in ViewPager NullPointer : 'void maps.ad.y.v()' on a null object reference

146 views Asked by At

I want to have a MapView in a Fragment. It seems simple... still I don't get it !

I get this error when launching the application :

java.lang.NullPointerException: Attempt to invoke interface method 'void maps.ad.y.v()' on a null object reference

I have an activity with a ViewPager : MainActivity

public class MainActivity extends AppCompatActivity {

  private SectionsPagerAdapter mSectionsPagerAdapter;
  private ViewPager mViewPager;

  //...onCreate(), etc

  public class SectionsPagerAdapter extends FragmentPagerAdapter {
    //...
    @Override
    public Fragment getItem(int position) {
      if (position == 0) {

        return ClassicFragment.newInstance(position + 1);
        // no problem with this one

      } else if (position == 1) {

        return MyMapFragment.newInstance(position + 1);
        // here is my problem

      }
    }
  }   
}

And 2 fragment. (one simple, and one with a Map) The Fragment I don't manage to make properly : MyMapFragment

public class MyMapFragment extends SupportMapFragment implements OnMapReadyCallback {

    View rootView;
    private MapView mapView;
    private GoogleMap googleMap;

    public MyMapFragment() {
    }

    public static MyMapFragment newInstance(int sectionNumber) {
        MyMapFragment fragment = new MyMapFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_map, container, false);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        mapView = (MapView) rootView.findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);
        mapView.onResume();
        mapView.getMapAsync(this);//when you already implement OnMapReadyCallback in your fragment
    }

    @Override
    public void onMapReady(GoogleMap map) {
        googleMap = map;
    }
}

Finally, here is the xml (fragment_map.xml) for the MyMapFragment.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

Any idea what I'm doing wrong ?

2

There are 2 answers

1
Fundhor On BEST ANSWER

I found 2 solutions :

Solution 1:

Change SupportMapFragment to Fragment.

Solution 2: (I chose this one)

Add this call in the MyMapFragment.

super.onCreateView(inflater, container, savedInstanceState);
0
Safal Kumar Ghimire On

Hope this will help you. Take a look.

public class MyActivity extends AppCompactActivity {


private TabLayout tabLayout;
private ViewPager viewPager;
Toolbar mToolBar;
FontIcon backBtnToolbar, nextBtnToolbar;
RobotoRegularTextView titleToolbar;
private PopupWindow settingPopUp;
private View popUpView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hotspot_tabs);





    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    initToolbar();
    initPopUpView();
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new MapFragment(), "Map");
    adapter.addFragment(new Map1Fragment(), "Map1");
    adapter.addFragment(new Map2Fragment(), "Map2");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

}

And use MapFragment as

public class MapFragment extends Fragment
    {
MapView mapView;
GoogleMap mMap;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // inflat and return the layout

    View v = inflater.inflate(R.layout.map_fragment_layout, container,
            false);

    switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity())) {
        case ConnectionResult.SUCCESS:
            Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT).show();
            mapView = (MapView) v.findViewById(R.id.map);
            mapView.onCreate(savedInstanceState);

            if (mapView != null) {
                mMap = mapView.getMap();
                mMap.getUiSettings().setMyLocationButtonEnabled(true);
                mMap.animateCamera(CameraUpdateFactory.zoomTo(17.0f));



            }
            break;
        case ConnectionResult.SERVICE_MISSING:
            Toast.makeText(getActivity(), "SERVICE MISSING", Toast.LENGTH_SHORT).show();
            break;
        case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
            Toast.makeText(getActivity(), "UPDATE REQUIRED", Toast.LENGTH_SHORT).show();
            break;
        default:
            Toast.makeText(getActivity(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()), Toast.LENGTH_SHORT).show();
    }



    return v;
}

}

   <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />