gpt4 book ai didi

java - 使用 GoogleMaps API 的 NULL 指针

转载 作者:行者123 更新时间:2023-11-30 02:40:36 25 4
gpt4 key购买 nike

我试图找到我的坐标并使用此代码在 map fragment 中显示它,但我的应用程序崩溃了。我正在为其使用最新的 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 图

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

最佳答案

我找到了!我的问题是我忘了添加

 class="com.google.android.gms.maps.SupportMapFragment"

在我的 map XML 文件中。希望这会对某人有所帮助

关于java - 使用 GoogleMaps API 的 NULL 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25814349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com