-1

我有一个用于查找用户当前位置的代码。当我在 android 1.6 中运行我的代码时,我得到了输出。但是我在 android 2.3 中运行我只得到了错误。我的代码有什么问题。

日志猫:

08-15 23:03:39.612: E/AndroidRuntime(403): FATAL EXCEPTION: main
08-15 23:03:39.612: E/AndroidRuntime(403): java.lang.NullPointerException
08-15 23:03:39.612: E/AndroidRuntime(403):  at com.android.locationspeecher.MyPositionOverlay.draw(MyPositionOverlay.java:25)
08-15 23:03:39.612: E/AndroidRuntime(403):  at com.google.android.maps.Overlay.draw(Overlay.java:179)
08-15 23:03:39.612: E/AndroidRuntime(403):  at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:45)
08-15 23:03:39.612: E/AndroidRuntime(403):  at com.google.android.maps.MapView.onDraw(MapView.java:530)
08-15 23:03:39.612: E/AndroidRuntime(403):  at android.view.View.draw(View.java:6880)
08-15 23:03:39.612: E/AndroidRuntime(403):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
08-15 23:03:39.612: E/AndroidRuntime(403):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-15 23:03:39.612: E/AndroidRuntime(403):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-15 23:03:39.612: E/AndroidRuntime(403):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-15 23:03:39.612: E/AndroidRuntime(403):  at android.view.View.draw(View.java:6883)
08-15 23:03:39.612: E/AndroidRuntime(403):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
08-15 23:03:39.612: E/AndroidRuntime(403):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
08-15 23:03:39.612: E/AndroidRuntime(403):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-15 23:03:39.612: E/AndroidRuntime(403):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-15 23:03:39.612: E/AndroidRuntime(403):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-15 23:03:39.612: E/AndroidRuntime(403):  at android.view.View.draw(View.java:6883)
08-15 23:03:39.612: E/AndroidRuntime(403):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
08-15 23:03:39.612: E/AndroidRuntime(403):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
08-15 23:03:39.612: E/AndroidRuntime(403):  at android.view.ViewRoot.draw(ViewRoot.java:1522)
08-15 23:03:39.612: E/AndroidRuntime(403):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
08-15 23:03:39.612: E/AndroidRuntime(403):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
08-15 23:03:39.612: E/AndroidRuntime(403):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 23:03:39.612: E/AndroidRuntime(403):  at android.os.Looper.loop(Looper.java:130)
08-15 23:03:39.612: E/AndroidRuntime(403):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-15 23:03:39.612: E/AndroidRuntime(403):  at java.lang.reflect.Method.invokeNative(Native Method)
08-15 23:03:39.612: E/AndroidRuntime(403):  at java.lang.reflect.Method.invoke(Method.java:507)
08-15 23:03:39.612: E/AndroidRuntime(403):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-15 23:03:39.612: E/AndroidRuntime(403):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-15 23:03:39.612: E/AndroidRuntime(403):  at dalvik.system.NativeStart.main(Native Method)

这是我的 MyPositionOverlay 代码:

public class MyPositionOverlay extends Overlay {

    Location location;
    private final int mRadius=5;

    @Override
    public void draw(Canvas canvas,MapView mapView,boolean shadow){

        Projection projection=mapView.getProjection();

        if(shadow==false){
            Double latitude=location.getLatitude()*1E6;
            Double longitude=location.getLongitude()*1E6;

            GeoPoint geoPoint;
             geoPoint=new GeoPoint(latitude.intValue(),longitude.intValue());

             Point point=new Point();
             projection.toPixels(geoPoint, point);

             RectF oval=new RectF(point.x - mRadius,point.y - mRadius,point.x + mRadius,point.y + mRadius);

             Paint paint=new Paint();
             paint.setARGB(250, 255, 255, 255);
             paint.setAntiAlias(true);
             paint.setFakeBoldText(true);

             Paint backPaint=new Paint();
             backPaint.setARGB(175, 50, 50, 50);
             backPaint.setAntiAlias(true);

             RectF backRect=new RectF(point.x + 2 + mRadius, point.y - 3*mRadius,point.x + 65, point.y + mRadius);

             canvas.drawOval(oval, paint);
             canvas.drawRoundRect(backRect, 5, 5, backPaint);
             canvas.drawText("Here I Am", point.x + 2*mRadius, point.y, paint);
        }
        super.draw(canvas, mapView, shadow);


        }

    @Override
    public boolean onTap(GeoPoint point,MapView mapView){
        return false;
    }


public Location getLocation(){

    return location;

}
public void setLocation(Location location){
    this.location=location;
}


}
4

1 回答 1

1

你有一个空指针异常。看起来你试图绘制的东西是空的。你有你的drawables在一个特定的分辨率文件夹吗?像drawable-ldpi。

如果是这种情况,2.3 模拟器可能具有不同的分辨率配置文件(可能是 mdpi),导致该可绘制对象为空。

无论如何,你需要去那行代码找出什么是空的,然后找出原因。

于 2012-08-15T17:42:00.553 回答