0

您好,我正在尝试使用逐项叠加在地图上绘制纬度和经度数组。我收到以下错误。可能是什么问题..????

>

 03-18 23:52:06.834: W/dalvikvm(2949): threadid=1: thread exiting with uncaught exception (group=0x40b01428)
03-18 23:52:06.844: E/AndroidRuntime(2949): FATAL EXCEPTION: main
03-18 23:52:06.844: E/AndroidRuntime(2949): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rahul.besttracker/com.rahul.besttracker.Mapview}: java.lang.NullPointerException
03-18 23:52:06.844: E/AndroidRuntime(2949):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at android.app.ActivityThread.access$600(ActivityThread.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at android.os.Handler.dispatchMessage(Handler.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at android.os.Looper.loop(Looper.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at android.app.ActivityThread.main(ActivityThread.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at java.lang.reflect.Method.invokeNative(Native Method)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at java.lang.reflect.Method.invoke(Method.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at dalvik.system.NativeStart.main(Native Method)
03-18 23:52:06.844: E/AndroidRuntime(2949): Caused by: java.lang.NullPointerException
03-18 23:52:06.844: E/AndroidRuntime(2949):     at com.google.android.maps.ItemizedOverlay.populate(ItemizedOverlay.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at com.rahul.besttracker.HelloItemizedOverlay.addOverlay(HelloItemizedOverlay.java:37)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at com.rahul.besttracker.Mapview.onCreate(Mapview.java:56)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at android.app.Activity.performCreate(Activity.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     ... 12 more
03-18 23:52:07.935: D/Process(2949): killProcess, pid=2949

这是我的 HelloItemizedOverlay 类

package com.rahul.besttracker;

    import java.util.ArrayList;

    import android.app.AlertDialog;
    import android.content.Context;
    import android.graphics.drawable.Drawable;

     import com.google.android.maps.ItemizedOverlay;
    import com.google.android.maps.OverlayItem;

    public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;

public HelloItemizedOverlay(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
    // TODO Auto-generated constructor stub
}

public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
    super(boundCenterBottom(defaultMarker));
    mContext = context;
}

@Override
protected OverlayItem createItem(int i) {
    return mOverlays.get(i);
}

@Override
public int size() {
    return mOverlays.size();
}

public void addOverlay(OverlayItem overlay) {

    mOverlays.add(overlay);
    populate();   <--------------GETTING ERROR HERE
}

@Override
protected boolean onTap(int index) {
    OverlayItem item = mOverlays.get(index);
    AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
    dialog.setTitle(item.getTitle());
    dialog.setMessage(item.getSnippet());
    dialog.show();
    return true;
}
    }

这是我的 MapView 类

package com.rahul.besttracker;

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

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

import android.graphics.drawable.Drawable;
import android.os.Bundle;

public class Mapview extends MapActivity {
MapView map;
ArrayList<String> mArrayList;
double[] Lat;
double[] Lon;

String stops[];
int latarr[], lonarr[];
OverlayItem overlayitem;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.maps);

    map = (MapView) findViewById(R.id.Map);
    // map.setBuiltInZoomControls(true);
    // setContentView(R.layout.maps);
    Bundle gotBasket = getIntent().getExtras();
    mArrayList = gotBasket.getStringArrayList("stops");
    Lat = gotBasket.getDoubleArray("Lat");
    Lon = gotBasket.getDoubleArray("Lon");

    System.out.println("SIZE+++" + Lat.length + " " + Lon.length);
    for (int i = 0; i < mArrayList.size(); i++) {
        System.out.println(" \n" + Lat[i] + "\t" + Lon[i]);
    }
    // stops=(String[]) mArrayList.toArray();

    List<Overlay> mapOverlays = map.getOverlays();
    Drawable drawable = this.getResources().getDrawable(
            R.drawable.mark_blue);
    HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
            drawable, this);

    for (int i = 0; i < mArrayList.size(); i++) {

        if (Lat[i] > 0 && Lon[i] > 0) {
            GeoPoint point = new GeoPoint((int) (Lat[i] * 1E6),
                    (int) (Lon[i] * 1E6));
            overlayitem = new OverlayItem(point, mArrayList.get(i), null);
        }
        itemizedoverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedoverlay);
    }
}

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

}

4

1 回答 1

0

在 onCreate 的 for 循环中,如果您不进入 if 语句,您将永远不会实例化覆盖项,因此您将向覆盖添加一个空指针。这最终会导致崩溃。add 应该是 if 的一部分。

于 2013-03-18T18:42:34.987 回答