我调用片段内的地图。它适用于我的智能手机,即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$1.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)
提前感谢您的帮助。
答案 0 :(得分:0)
尝试使用 getMapAsync(this)而不是 getMap()。
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));
希望这有帮助!
编辑:
您可以查看有关处理地图的Google文档。
https://developers.google.com/android/reference/com/google/android/gms/maps/MapFragment?hl=en
您可以尝试使用以下代码创建标记。
using WindowsRuntimeComponent1;