添加Google地图标记时,Android应用会崩溃

时间:2015-03-26 07:45:31

标签: android google-maps

我目前正在努力在Google地图上添加一些标记,但每当我尝试向地图添加标记时,该应用都会崩溃。 这是我写的代码

createMapView();
googleMap.addMarker(new MarkerOptions()
    .position(new LatLng(10, 10))
    .title("Hello world"));

createMapView()方法定义为:

private void createMapView(){

    try {
        if(null == googleMap){



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

            if(null == googleMap) {
                Toast.makeText(getApplicationContext(),
                        "Error creating map", Toast.LENGTH_SHORT).show();
            }
        }
    } catch (NullPointerException exception){
        Log.e("mapApp", exception.toString());
    }

createMapView()方法工作正常。

但是,每当我在googleMap对象上调用addMarker方法时,应用程序就会崩溃。

如果您需要检查我的清单文件:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".ResultActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <!-- Goolge Maps API Key -->
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="xxxxxMyKeyxxxxx" />
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

</application>

以下是我在应用崩溃时获得的日志:

03-26 08:34:46.445  28626-28626/com.example.omarlahlou.comforttrain E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.omarlahlou.comforttrain, PID: 28626
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.omarlahlou.comforttrain/com.example.omarlahlou.comforttrain.ResultActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5292)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.example.omarlahlou.comforttrain.ResultActivity.onCreate(ResultActivity.java:47)
        at android.app.Activity.performCreate(Activity.java:5264)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5292)
        at java.lang.reflect.Method.invokeNative(Native Method)

1 个答案:

答案 0 :(得分:0)

使用此代码:

if(googleMap!=null){
googleMap.addMarker(new MarkerOptions()
    .position(new LatLng(10, 10))
    .title("Hello world"));
}

http://www.tutorialspoint.com/android/android_google_maps.html https://developers.google.com/maps/documentation/android/marker