GetMap() return null on Android API > 15 但在相同或更低版本上工作得很好
GetMap() return null on Android API > 15 but works perfectly fine on equal or lower version
我调用片段内的地图。它适用于我的 API 15(Jelly Bean)智能手机,但不适用于更高 API。
我收到 NullPointerException,应用程序崩溃。我认为问题出在我尝试 getMap() 时布局尚未准备好。我尝试了各种各样的事情,但没有任何效果。
我还看到 getMap() 方法最近已被弃用。
这是我的部分代码
布局:
<FrameLayout
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"
tools:context="fr.irseem.ping32.smartp3.fragments.LocationFragment">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map_view"
tools:context="fr.irseem.ping32.MapsActivity"
android:name="com.google.android.gms.maps.MapFragment" />
</FrameLayout>
我放地图的LocationFragment :
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_location, container, false);
// Initializing array List
markerPoints = new ArrayList<LatLng>();
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map_view);
mapFragment.getMapAsync(this);
setUpMapIfNeeded();
// Inflate the layout for this fragment
return v;
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((MapFragment) this.getFragmentManager().findFragmentById(R.id.map_view))
.getMap();
mMap.setMyLocationEnabled(true);
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location arg0) {
// TODO Auto-generated method stub
LatLng currentLocation = new LatLng(arg0.getLatitude(), arg0.getLongitude());
MarkerOptions currentLocationMarkOpt = new MarkerOptions().position(currentLocation).title("My location");
// on remplace le marqueur par notre nouvelle position
currentLocationMark.setPosition(currentLocation);
//currentLocationMark = mMap.addMarker(currentLocationMarkOpt);
markerPoints.add(1, currentLocation);
updateItinerary();
}
});
}
}
}
这里是 Logcat :
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: FATAL EXCEPTION: main
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: Process: fr.irseem.ping32.smartp3, PID: 11418
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at fr.irseem.ping32.smartp3.fragments.LocationFragment.onCreateView(LocationFragment.java:149)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at android.app.Fragment.performCreateView(Fragment.java:2220)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at android.app.BackStackRecord.run(BackStackRecord.java:793)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at android.app.FragmentManagerImpl.run(FragmentManager.java:482)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
提前感谢您的帮助。
尝试使用 getMapAsync(this) 而不是 getMap().
mMap = ((MapFragment) this.getFragmentManager().findFragmentById(R.id.map_view));
mMap.getMapAsync(this);
希望对您有所帮助!
编辑:
您或许可以查看有关处理地图的 Google 文档。
https://developers.google.com/android/reference/com/google/android/gms/maps/MapFragment?hl=en
您可以尝试使用以下代码创建标记。
Geocoder geocoder = new Geocoder(context);
Origin_Map = extras.getString(MainActivity.ORIGIN_MAP);
Destination_Map = extras.getString(MainActivity.DESTINATION_MAP);
Addr_Origin = geocoder.getFromLocationName(Origin_Map, 1);
Addr_Dest = geocoder.getFromLocationName(Destination_Map, 1);
if (Addr_Origin.size() > 0) {
latitude_origin = Addr_Origin.get(0).getLatitude();
longitude_origin = Addr_Origin.get(0).getLongitude();
}
if (Addr_Dest.size() > 0) {
latitude_destination = Addr_Dest.get(0).getLatitude();
longitude_destination = Addr_Dest.get(0).getLongitude();
}
Marker m1 = googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude_origin, longitude_origin)).title(Origin_Map).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
Marker m2 = googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude_destination, longitude_destination)).title(Destination_Map).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
PolylineOptions flightpath = new PolylineOptions().add(new LatLng(latitude_origin, longitude_origin)).add(new LatLng(latitude_destination, longitude_destination));
Polyline flightpath_polyline = googleMap.addPolyline(flightpath.width(5).color(Color.BLUE).geodesic(true));
我调用片段内的地图。它适用于我的 API 15(Jelly Bean)智能手机,但不适用于更高 API。 我收到 NullPointerException,应用程序崩溃。我认为问题出在我尝试 getMap() 时布局尚未准备好。我尝试了各种各样的事情,但没有任何效果。 我还看到 getMap() 方法最近已被弃用。
这是我的部分代码
布局:
<FrameLayout
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"
tools:context="fr.irseem.ping32.smartp3.fragments.LocationFragment">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map_view"
tools:context="fr.irseem.ping32.MapsActivity"
android:name="com.google.android.gms.maps.MapFragment" />
</FrameLayout>
我放地图的LocationFragment :
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_location, container, false);
// Initializing array List
markerPoints = new ArrayList<LatLng>();
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map_view);
mapFragment.getMapAsync(this);
setUpMapIfNeeded();
// Inflate the layout for this fragment
return v;
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((MapFragment) this.getFragmentManager().findFragmentById(R.id.map_view))
.getMap();
mMap.setMyLocationEnabled(true);
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location arg0) {
// TODO Auto-generated method stub
LatLng currentLocation = new LatLng(arg0.getLatitude(), arg0.getLongitude());
MarkerOptions currentLocationMarkOpt = new MarkerOptions().position(currentLocation).title("My location");
// on remplace le marqueur par notre nouvelle position
currentLocationMark.setPosition(currentLocation);
//currentLocationMark = mMap.addMarker(currentLocationMarkOpt);
markerPoints.add(1, currentLocation);
updateItinerary();
}
});
}
}
}
这里是 Logcat :
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: FATAL EXCEPTION: main
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: Process: fr.irseem.ping32.smartp3, PID: 11418
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at fr.irseem.ping32.smartp3.fragments.LocationFragment.onCreateView(LocationFragment.java:149)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at android.app.Fragment.performCreateView(Fragment.java:2220)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at android.app.BackStackRecord.run(BackStackRecord.java:793)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at android.app.FragmentManagerImpl.run(FragmentManager.java:482)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-20 15:24:25.861 11418-11418/fr.irseem.ping32.smartp3 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
提前感谢您的帮助。
尝试使用 getMapAsync(this) 而不是 getMap().
mMap = ((MapFragment) this.getFragmentManager().findFragmentById(R.id.map_view));
mMap.getMapAsync(this);
希望对您有所帮助!
编辑:
您或许可以查看有关处理地图的 Google 文档。
https://developers.google.com/android/reference/com/google/android/gms/maps/MapFragment?hl=en
您可以尝试使用以下代码创建标记。
Geocoder geocoder = new Geocoder(context);
Origin_Map = extras.getString(MainActivity.ORIGIN_MAP);
Destination_Map = extras.getString(MainActivity.DESTINATION_MAP);
Addr_Origin = geocoder.getFromLocationName(Origin_Map, 1);
Addr_Dest = geocoder.getFromLocationName(Destination_Map, 1);
if (Addr_Origin.size() > 0) {
latitude_origin = Addr_Origin.get(0).getLatitude();
longitude_origin = Addr_Origin.get(0).getLongitude();
}
if (Addr_Dest.size() > 0) {
latitude_destination = Addr_Dest.get(0).getLatitude();
longitude_destination = Addr_Dest.get(0).getLongitude();
}
Marker m1 = googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude_origin, longitude_origin)).title(Origin_Map).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
Marker m2 = googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude_destination, longitude_destination)).title(Destination_Map).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
PolylineOptions flightpath = new PolylineOptions().add(new LatLng(latitude_origin, longitude_origin)).add(new LatLng(latitude_destination, longitude_destination));
Polyline flightpath_polyline = googleMap.addPolyline(flightpath.width(5).color(Color.BLUE).geodesic(true));