0

我的程序导致空指针异常...我认为问题在于将 lat3 和 lon3 从 double 转换为 int...是否有其他方法可以将 double 中的位置解析为 int 以将其提供给 GeoPoint?

代码:

public class mapLoc extends com.google.android.maps.MapActivity implements LocationListener
{
    TextView t ;
    int lat2,lon2,lat3,lon3;
    Double presentlon,presentlat;

    Drawable d;
    MapView mv;
    MapController mc;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.map);

        t = (TextView)findViewById(R.id.maptxt);

        Intent intent = getIntent();
        Bundle extra = intent.getExtras();

     String latitude = extra.getString("latitude");
     String longitude = extra.getString("longitude");

     t.append(latitude+"\t");
     t.append(longitude);
     Float lat1 = Float.parseFloat(latitude);
     Float lon1 = Float.parseFloat(longitude);
        lat2 = (int) (lat1*1E6);   
        lon2 = (int) (lon1*1E6); 


        mv = (MapView)findViewById(R.id.maps);

        mc = mv.getController();
        mc.setZoom(14);
        mv.setBuiltInZoomControls(true);
        mv.displayZoomControls(true);
        mv.setClickable(true);


     List<Overlay> mapOverlays = mv.getOverlays();
     Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
     overlay itemizedoverlay = new overlay(drawable, this);

     lat3 = (int)(presentlat * 1E6);
     lon3 = (int)(presentlon * 1E6);


     GeoPoint point = new GeoPoint(lat3, lon3);

     OverlayItem overlayitem = new OverlayItem(point, "Hello, user", "This is your present location");
     mc.animateTo(point);

     itemizedoverlay.addOverlay(overlayitem);
     mapOverlays.add(itemizedoverlay);

     GeoPoint point2 = new GeoPoint(lat2, lon2);
     OverlayItem overlayitem2 = new OverlayItem(point2, "This is your","saved location");
     itemizedoverlay.addOverlay(overlayitem2);
     mapOverlays.add(itemizedoverlay);



    }


    @Override
    public void onLocationChanged(Location location)
    {
     presentlat = location.getLatitude();
     presentlon =  location.getLongitude();

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }


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



}

Logcat 错误:

05-25 10:21:52.444: W/dalvikvm(342): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-25 10:21:52.444: E/AndroidRuntime(342): Uncaught handler: thread main exiting due to uncaught exception
05-25 10:21:52.454: E/AndroidRuntime(342): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.loc/com.loc.mapLoc}: java.lang.NullPointerException
05-25 10:21:52.454: E/AndroidRuntime(342):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
05-25 10:21:52.454: E/AndroidRuntime(342):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-25 10:21:52.454: E/AndroidRuntime(342):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-25 10:21:52.454: E/AndroidRuntime(342):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-25 10:21:52.454: E/AndroidRuntime(342):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 10:21:52.454: E/AndroidRuntime(342):  at android.os.Looper.loop(Looper.java:123)
05-25 10:21:52.454: E/AndroidRuntime(342):  at android.app.ActivityThread.main(ActivityThread.java:4363)
05-25 10:21:52.454: E/AndroidRuntime(342):  at java.lang.reflect.Method.invokeNative(Native Method)
05-25 10:21:52.454: E/AndroidRuntime(342):  at java.lang.reflect.Method.invoke(Method.java:521)
05-25 10:21:52.454: E/AndroidRuntime(342):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-25 10:21:52.454: E/AndroidRuntime(342):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-25 10:21:52.454: E/AndroidRuntime(342):  at dalvik.system.NativeStart.main(Native Method)
05-25 10:21:52.454: E/AndroidRuntime(342): Caused by: java.lang.NullPointerException
05-25 10:21:52.454: E/AndroidRuntime(342):  at com.loc.mapLoc.onCreate(mapLoc.java:71)
05-25 10:21:52.454: E/AndroidRuntime(342):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-25 10:21:52.454: E/AndroidRuntime(342):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
4

3 回答 3

1

在代码片段中,我认为这些 presentlat 和 presentlon 没有被初始化并因此抛出 NPE。

 lat3 = (int)(presentlat * 1E6); // NPE
 lon3 = (int)(presentlon * 1E6);

在使用前初始化它们或进行空检查。

于 2012-05-25T05:02:47.813 回答
1

请记住,Double(大写 D) 不是原始类型,而是碰巧只能保存装箱double(注意区别)值的引用类型 - 或null. Java 语言对拆箱的处理方式与通过引用访问其他对象的方式不同。如果引用是null(这显然是这里的情况),它会抛出一个 NPE。拆箱的真正区别在于,这种访问是通过ref.doubleValue()在算术表达式中使用引用时隐式发生的。

于 2012-05-25T05:16:57.250 回答
0

你是对的,你的应用程序正在爆炸,因为 presentlat 和 presentlon 是尚未初始化的私有变量。您应该将该代码移动到您的 onLocationChanged 函数中。

于 2012-05-25T05:08:04.190 回答