English 中文(简体)
此程序正在显示无效指针例外... 。 将双向整数转换为整数
原标题:The program is showing null pointer exception....converting double to int

我的程式造成了一个无效的指针例外... 我认为问题在于将 lat3 和 lon3 从双倍转换到 int...

守则:

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.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;
    };



}

日志错误 :

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)
最佳回答

在代码片段里 我不认为这些现在的和现在的 正在被初始化,因此投掷NPE。

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

使用前先初始化它们, 或进行无效检查 。

问题回答

切记 < code> Double (资本 D) 不是一个原始类型,而是一个碰巧只能持有框框的 < code> two (注意差异) 值的引用类型, 或 noll (注意差异) 。 Java 语言不会通过引用处理与其他对象存取器不同的无框。 如果引用是 < code> null (这里显然属于这种情况), 它会扔出 NPE 。 与不选框的真正区别在于, 当您在算术表达中使用引用时, 这种访问会隐含在 < code>ref. twobleValue () 中 。

您是对的, 您的应用程序正在爆炸, 因为 presentlat 和 presentlon 是尚未初始化的私人变量 。 您应该将该代码移动到您关于 Location Changed 的功能中 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签