gpt4 book ai didi

java - Android 谷歌地图致命异常

转载 作者:行者123 更新时间:2023-11-29 03:24:37 24 4
gpt4 key购买 nike

所以我正在尝试创建一个 android 应用程序,它将读取大量飞行数据并在 Google map 上用标记表示数据。

目前我无法做很多事情,我可以生成一个显示谷歌地图的应用程序,并且已经能够使用 XML 设置开始位置。

我似乎无法添加标记,它只是崩溃了。

package com.fly.plane;

import com.fly.plane.R;
import com.google.android.gms.maps.GoogleMap;

import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MyMapActivity extends FragmentActivity {
GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_map);

// without this next line, it will work:
mMap.addMarker(new MarkerOptions()
.position(new LatLng(0, 0))
.title("Hello world"));
}

}

和 XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

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

</RelativeLayout>

这是错误日志:

02-10 22:31:28.433: E/AndroidRuntime(13131): FATAL EXCEPTION: main
02-10 22:31:28.433: E/AndroidRuntime(13131): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fly.plane/com.fly.plane.MyMapActivity}: java.lang.NullPointerException
02-10 22:31:28.433: E/AndroidRuntime(13131): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
02-10 22:31:28.433: E/AndroidRuntime(13131): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
02-10 22:31:28.433: E/AndroidRuntime(13131): at android.app.ActivityThread.access$700(ActivityThread.java:159)
02-10 22:31:28.433: E/AndroidRuntime(13131): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
02-10 22:31:28.433: E/AndroidRuntime(13131): at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 22:31:28.433: E/AndroidRuntime(13131): at android.os.Looper.loop(Looper.java:137)
02-10 22:31:28.433: E/AndroidRuntime(13131): at android.app.ActivityThread.main(ActivityThread.java:5419)
02-10 22:31:28.433: E/AndroidRuntime(13131): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 22:31:28.433: E/AndroidRuntime(13131): at java.lang.reflect.Method.invoke(Method.java:525)
02-10 22:31:28.433: E/AndroidRuntime(13131): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
02-10 22:31:28.433: E/AndroidRuntime(13131): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
02-10 22:31:28.433: E/AndroidRuntime(13131): at dalvik.system.NativeStart.main(Native Method)
02-10 22:31:28.433: E/AndroidRuntime(13131): Caused by: java.lang.NullPointerException
02-10 22:31:28.433: E/AndroidRuntime(13131): at com.fly.plane.MyMapActivity.onCreate(MyMapActivity.java:21)
02-10 22:31:28.433: E/AndroidRuntime(13131): at android.app.Activity.performCreate(Activity.java:5372)
02-10 22:31:28.433: E/AndroidRuntime(13131): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
02-10 22:31:28.433: E/AndroidRuntime(13131): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
02-10 22:31:28.433: E/AndroidRuntime(13131): ... 11 more

我在 java/android 开发方面不是很擅长,所以对于导致这种情况的任何帮助将不胜感激。

问候

菲尔

最佳答案

少了一行:

mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

你的 onCreate() 方法必须像这样:

private GoogleMap mMap; 

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_map);

//Add this line.
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

mMap.addMarker(new MarkerOptions()
.position(new LatLng(0, 0))
.title("Hello world"));
}

关于java - Android 谷歌地图致命异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21689341/

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