我试图找到我的坐标并使用此代码在地图片段中显示它但我的应用程序崩溃了。我正在使用最新的GoogleMap API。
public class gmapp extends FragmentActivity{
// Google Map
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
setUpIfNeeded();
}
private void setUpIfNeeded() {
if (googleMap == null)
{
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (googleMap != null)
{
setUpMap();
}
}
}
private void setUpMap() {
googleMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria , true);
Location myLocation = locationManager.getLastKnownLocation(provider);
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
double latitude = myLocation.getLatitude();
double longtitude = myLocation.getLongitude();
LatLng latLng = new LatLng(latitude , longtitude);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(20));
googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude , longtitude)).title("Im here"));
}
}
这是我编译后的错误信息。
09-12 20:10:01.034: E/AndroidRuntime(27507): FATAL EXCEPTION: main
09-12 20:10:01.034: E/AndroidRuntime(27507): Process: com.max.maxo, PID: 27507
09-12 20:10:01.034: E/AndroidRuntime(27507): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.max.maxo/com.max.maxo.gmapp}: java.lang.NullPointerException
09-12 20:10:01.034: E/AndroidRuntime(27507): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2394)
09-12 20:10:01.034: E/AndroidRuntime(27507): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2452)
09-12 20:10:01.034: E/AndroidRuntime(27507): at android.app.ActivityThread.access$900(ActivityThread.java:172)
09-12 20:10:01.034: E/AndroidRuntime(27507): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
09-12 20:10:01.034: E/AndroidRuntime(27507): at android.os.Handler.dispatchMessage(Handler.java:102)
09-12 20:10:01.034: E/AndroidRuntime(27507): at android.os.Looper.loop(Looper.java:136)
09-12 20:10:01.034: E/AndroidRuntime(27507): at android.app.ActivityThread.main(ActivityThread.java:5586)
09-12 20:10:01.034: E/AndroidRuntime(27507): at java.lang.reflect.Method.invokeNative(Native Method)
09-12 20:10:01.034: E/AndroidRuntime(27507): at java.lang.reflect.Method.invoke(Method.java:515)
09-12 20:10:01.034: E/AndroidRuntime(27507): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
09-12 20:10:01.034: E/AndroidRuntime(27507): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
09-12 20:10:01.034: E/AndroidRuntime(27507): at dalvik.system.NativeStart.main(Native Method)
09-12 20:10:01.034: E/AndroidRuntime(27507): Caused by: java.lang.NullPointerException
09-12 20:10:01.034: E/AndroidRuntime(27507): at com.max.maxo.gmapp.setUpIfNeeded(gmapp.java:35)
09-12 20:10:01.034: E/AndroidRuntime(27507): at com.max.maxo.gmapp.onCreate(gmapp.java:29)
09-12 20:10:01.034: E/AndroidRuntime(27507): at android.app.Activity.performCreate(Activity.java:5451)
09-12 20:10:01.034: E/AndroidRuntime(27507): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
09-12 20:10:01.034: E/AndroidRuntime(27507): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
09-12 20:10:01.034: E/AndroidRuntime(27507): ... 11 more
在我的XML中,我只获得了片段映射
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
答案 0 :(得分:1)
我找到了! 我的问题是我忘了添加
class="com.google.android.gms.maps.SupportMapFragment"
在我的地图XML文件中。 希望这会帮助某人