请帮助。当我在我的片段上打开我的应用程序时,我的应用程序崩溃了。我正在使用导航抽屉。
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setMyLocationEnabled(boolean)' on a null object reference
at johnbea.com.mysystem.LanlordMapFragment.onCreateView(LanlordMapFragment.java:68)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
这是导航抽屉上的代码,然后转到地图片段
private void displaySelectedScreen(int ID){
Fragment fragment = null;
switch (ID){
case R.id.add_apartment:
fragment = LandlordAddApartment.newInstance(id, username);
FragmentManager addfragmentManager = getSupportFragmentManager();
addfragmentManager.beginTransaction();
break;
case R.id.reserve_request:
fragment = new LandlordReservation();
break;
case R.id.ver_request:
fragment = LandlordVerification.newInstance(id, username);
FragmentManager verificationfragment = getSupportFragmentManager();
verificationfragment.beginTransaction();
break;
case R.id.landlord_log_out:
fragment = LandlordLogout.newInstance(id, username);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction();
break;
case R.id.map2:
fragment = new LanlordMapFragment();
}
if(fragment != null){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_landlord, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
这是我的片段,谷歌地图..我在获取此片段的当前位置时遇到麻烦。它已经花了我2个星期我仍然无法弄清楚..有人可以帮助我:(
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("MAP");
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mGoogleMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(bestProvider, 20000, 0, (android.location.LocationListener) getActivity());
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map1);
mapFragment.getMapAsync(this);
}
/*@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getContext());
mGoogleMap = googleMap;
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.addMarker(new MarkerOptions().position(new LatLng(13.137769, 123.743652)).title("Legazpi property").snippet("asdasdasd"));
CameraPosition legazpi = CameraPosition.builder().target(new LatLng(13.137769, 123.743652)).zoom(16).bearing(0).tilt(45).build();
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(legazpi));
// LatLng legazpi = new LatLng(-13.137769, 123.743652);
// mGoogleMap.addMarker(new MarkerOptions().position(legazpi).title("Legazpi property"));
// mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(legazpi));
//return;
}
* /
@Override
public void onLocationChanged(Location location) {
TextView locationTv = (TextView) view.findViewById(R.id.latlongLocation);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
mGoogleMap.addMarker(new MarkerOptions().position(latLng));
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
locationTv.setText("Latitude:" + latitude + ", Longitude:" + longitude);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
答案 0 :(得分:0)
John因为mGoogleMap
而未初始化,所以它为null。您需要调用onMapReady
函数。在onMapReady
函数中使用mGoogleMap.setMyLocationEnabled(true);
。
答案 1 :(得分:0)
在初始化地图
之后,在public voidmGoogleMap.setMyLocationEnabled(true);
中使用onMapReady
onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getContext());
mGoogleMap = googleMap;
mGoogleMap.setMyLocationEnabled(true);
如果您不想要地图删除mGoogleMap.setMyLocationEnabled(true);