Android Google Maps致命异常 [英] Android Google Maps Fatal Exception

查看:69
本文介绍了Android Google Maps致命异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试创建一个可读取大量飞行数据并使用标记表示Google地图上数据的android应用.

So i am trying to create a android app that will read in lots of flight data and represent the data on Google maps with markers.

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

Currently im stuck not being able to do much, i can generate a app that displays Google maps, and have been able to set a start location using XML.

虽然我似乎无法添加标记,但它崩溃了.

I can't seem to add markers though, it just crashes.

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开发,因此请对造成此问题的原因提供帮助.

Im not very strong at java/android development so any help on what is causing this would be appreciated.

致谢

菲尔

推荐答案

缺少一行:

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"));
}

这篇关于Android Google Maps致命异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆