ViewPager中的MapFragment NullPointer:' void maps.ad.y.v()'在null对象引用上

时间:2016-12-30 23:14:15

标签: android google-maps-android-api-2 supportmapfragment

我想在Fragment中有一个MapView。 这看起来很简单......但我还是得不到它!

启动应用程序时出现此错误:

  

java.lang.NullPointerException:尝试调用接口方法   ' void maps.ad.y.v()'在空对象引用上

我有一个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

      }
    }
  }   
}

和2个片段。 (一个简单,一个带有Map) 片段我没有设法正确制作: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;
    }
}

最后,这是MyMapFragment的xml(fragment_map.xml)。

<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>

知道我做错了吗?

2 个答案:

答案 0 :(得分:3)

我找到了2个解决方案:

解决方案1:

SupportMapFragment 更改为 Fragment

解决方案2 :(我选择了这个)

在MyMapFragment中添加此调用。

super.onCreateView(inflater, container, savedInstanceState);

答案 1 :(得分:1)

希望这会对你有所帮助。看一看。

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);
    }
}

}

使用MapFragment作为

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" />