1

在这个活动中,我从数据库中的任务表中获取纬度和经度,然后我将它们放入带有标记图像的地图中:

我在日志猫中有一个强制关闭,出现空指针异常...

public class MaptoDo extends MapActivity{
MapView mv;
MapController mc;
SQLiteDatabase sql;
Cursor c;
GeoPoint p;
double [] lat;
double[]lon;
List<Overlay> listOfOverlays;
  @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent in =getIntent();
        mv=(MapView) findViewById(R.id.mapView);
        mv.setBuiltInZoomControls(true);
        mv.setSatellite(true);
        mv.setTraffic(true);
        sql=openOrCreateDatabase("db",0,null);
        c=sql.rawQuery("select * from Mission",null);
        if(c.getColumnCount()!=0){
            int i=0;
            while(c.moveToNext()!=false){
                lat[i]=c.getDouble(3);
                lon[i]=c.getDouble(4);
            }
            }
        for(int i=0;i<lat.length;i++)
        {
            p=new GeoPoint((int)lat[i]*1000000,(int) lon[i]*1000000);
            mc.animateTo(p);
            mc.setZoom(8);
            MapOverlay mapOverlay = new MapOverlay();
            listOfOverlays = mv.getOverlays();
            listOfOverlays.clear();
            listOfOverlays.add(mapOverlay);
            mv.invalidate();

        }
    }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    MenuItem itIn=menu.add("Zoom in");
    itIn.setIcon(R.drawable.in);
    MenuItem itOut=menu.add("Zoom out");
    itOut.setIcon(R.drawable.out);
    return super.onCreateOptionsMenu(menu);

  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    if(item.getTitle()=="Zoom in")
    {
        MapController mc=mv.getController();
        mc.zoomIn();
    }
    else if(item.getTitle()=="Zoom out")
    {
        MapController mc=mv.getController();
        mc.zoomOut();
    }


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


public class MapOverlay extends com.google.android.maps.Overlay
{
    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
            long when) {
        super.draw(canvas, mapView, shadow);
        Point screenPts = new Point();
        mapView.getProjection().toPixels(p, screenPts);
        Bitmap bmp = BitmapFactory.decodeResource(
        getResources(), R.drawable.redpushpin);
        canvas.drawBitmap(bmp, screenPts.x-20, screenPts.y-34, null);
        return true;
    }
    @Override
    public boolean onTouchEvent(MotionEvent event, MapView mapView)
    {
    if (event.getAction() == 1) {
    GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY());
    Toast.makeText(getBaseContext(),"Location: "+p.getLatitudeE6() / 1E6 + "," +
    p.getLongitudeE6() /1E6 ,Toast.LENGTH_SHORT).show();
    }
            return false;

    }
}

}

这是 logcat 的踪迹:

08-14 00:43:33.068: E/AndroidRuntime(652): FATAL EXCEPTION: main
  08-14 00:43:33.068: E/AndroidRuntime(652): java.lang.RuntimeException: Unable to start activity ComponentInfo{sarah.android/sarah.android.MaptoDo}: java.lang.NullPointerException
   08-14 00:43:33.068: E/AndroidRuntime(652):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
   08-14 00:43:33.068: E/AndroidRuntime(652):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
   08-14 00:43:33.068: E/AndroidRuntime(652):   at android.app.ActivityThread.access$1500(ActivityThread.java:117)
   08-14 00:43:33.068: E/AndroidRuntime(652):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
   08-14 00:43:33.068: E/AndroidRuntime(652):   at android.os.Handler.dispatchMessage(Handler.java:99)
  08-14 00:43:33.068: E/AndroidRuntime(652):    at android.os.Looper.loop(Looper.java:130)
   08-14 00:43:33.068: E/AndroidRuntime(652):   at android.app.ActivityThread.main(ActivityThread.java:3683)
  08-14 00:43:33.068: E/AndroidRuntime(652):    at java.lang.reflect.Method.invokeNative(Native Method)
  08-14 00:43:33.068: E/AndroidRuntime(652):    at java.lang.reflect.Method.invoke(Method.java:507)
  08-14 00:43:33.068: E/AndroidRuntime(652):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
   08-14 00:43:33.068: E/AndroidRuntime(652):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  08-14 00:43:33.068: E/AndroidRuntime(652):    at dalvik.system.NativeStart.main(Native Method)
 08-14 00:43:33.068: E/AndroidRuntime(652): Caused by: java.lang.NullPointerException
 08-14 00:43:33.068: E/AndroidRuntime(652):     at sarah.android.MaptoDo.onCreate(MaptoDo.java:40)
 08-14 00:43:33.068: E/AndroidRuntime(652):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 08-14 00:43:33.068: E/AndroidRuntime(652):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
4

1 回答 1

2
lat[i]=c.getDouble(3);
lon[i]=c.getDouble(4);

看起来你没有初始化你的数组,你只是在顶部声明了它们:

double [] lat;
double [] lon;

您必须实例化它们,例如:

double [] lat = new double[5];
double [] lon = new double[5];

您还需要mc在调用变量之前对其进行实例化。你可以像这样实例化它:

mc = mv.getController();
于 2012-08-13T21:53:01.767 回答