gpt4 book ai didi

java - 程序显示空指针异常....将 double 转换为 int

转载 作者:行者123 更新时间:2023-12-02 07:46:33 25 4
gpt4 key购买 nike

我的程序导致空指针异常...我认为问题在于将 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)

最佳答案

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

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

要么在使用前初始化它们,要么进行空检查。

关于java - 程序显示空指针异常....将 double 转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10748650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com