无法在谷歌地图中显示精确定位

时间:2012-08-26 09:03:36

标签: android google-maps

我正在Google地图上开发一个Android应用程序。我已经在模拟器中的applciation中成功显示了MapView。但是当我试图在地图上插入精确标记时,出现了以下有关“无法启动活动ComponentInfo”的问题。 这是我的代码:

package com.example.maps;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

公共类MainActivity扩展了MapActivity {

MapView map;
MapController controller;
Drawable d;
List<Overlay> overlayList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    map = (MapView)findViewById(R.id.mapRef);
    map.setBuiltInZoomControls(true);

    controller = map.getController();
    GeoPoint point = new GeoPoint(23129491,72541321);
    controller.animateTo(point);
    controller.setZoom(19);

    d = getResources().getDrawable(R.drawable.taxi);
    OverlayItem overlayItem =  new OverlayItem(point, "Hello there!","This is the taxi");
    drawPinpoint draw = new drawPinpoint(d, MainActivity.this);
    draw.insertPinpoint(overlayItem);
    overlayList.add(draw);

}

static class drawPinpoint extends ItemizedOverlay<OverlayItem>{

    private ArrayList<OverlayItem> pinpoints = new ArrayList<OverlayItem>();
    private Context c;

    public drawPinpoint(Drawable arg0) {
        super(boundCenter(arg0));
        // TODO Auto-generated constructor stub
    }

    public drawPinpoint(Drawable m, Context context) {
        // TODO Auto-generated constructor stub
        this(m);
        c = context;
    }

    @Override
    protected OverlayItem createItem(int i) {
        // TODO Auto-generated method stub
        return pinpoints.get(i);
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return pinpoints.size();
    }

    public void insertPinpoint(OverlayItem item){
        pinpoints.add(item);
        this.populate();
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

}

这是我的清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.maps"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
<uses-library android:name="com.google.android.maps"/>
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

这是我对NullPounterException的堆栈跟踪:

08-26 14:19:11.760:W / dalvikvm(528):threadid = 1:线程退出时未捕获异常(group = 0x4001d800) 08-26 14:19:11.790:E / AndroidRuntime(528):致命异常:主要 08-26 14:19:11.790:E / AndroidRuntime(528):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.maps / com.example.maps.MainActivity}:java.lang.NullPointerException 08-26 14:19:11.790:E / AndroidRuntime(528):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 08-26 14:19:11.790:E / AndroidRuntime(528):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 08-26 14:19:11.790:E / AndroidRuntime(528):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125) 08-26 14:19:11.790:E / AndroidRuntime(528):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033) 08-26 14:19:11.790:E / AndroidRuntime(528):在android.os.Handler.dispatchMessage(Handler.java:99) 08-26 14:19:11.790:E / AndroidRuntime(528):在android.os.Looper.loop(Looper.java:123) 08-26 14:19:11.790:E / AndroidRuntime(528):在android.app.ActivityThread.main(ActivityThread.java:4627) 08-26 14:19:11.790:E / AndroidRuntime(528):at java.lang.reflect.Method.invokeNative(Native Method) 08-26 14:19:11.790:E / AndroidRuntime(528):at java.lang.reflect.Method.invoke(Method.java:521) 08-26 14:19:11.790:E / AndroidRuntime(528):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 08-26 14:19:11.790:E / AndroidRuntime(528):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 08-26 14:19:11.790:E / AndroidRuntime(528):at dalvik.system.NativeStart.main(Native Method) 08-26 14:19:11.790:E / AndroidRuntime(528):引起:java.lang.NullPointerException 08-26 14:19:11.790:E / AndroidRuntime(528):at com.example.maps.MainActivity.onCreate(MainActivity.java:42) 08-26 14:19:11.790:E / AndroidRuntime(528):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 08-26 14:19:11.790:E / AndroidRuntime(528):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 08-26 14:19:11.790:E / AndroidRuntime(528):... 11 more

1 个答案:

答案 0 :(得分:0)

我认为代码的第42行是:

overlayList.add(draw);

并且您尚未创建overlayList对象。