强制关闭地图控制器?

发布于 2024-11-13 10:34:34 字数 5592 浏览 2 评论 0原文

我有一个奇怪的问题。我想在地图上显示某个位置。问题是,有时一切正常,但有时我会强制关闭,在 DDMS 中我得到: 06-03 18:24:14.171:错误/AndroidRuntime(2228):在com.google.android.maps.MapController.setCenter(MapController.java:345) 看来问题出在这一行:mapView.getController().setCenter(pt)。我能理解,为什么有时一切都很好,有时却不太好。

这是我的代码:

package com.ShoppingList.Shops;


import java.io.IOException;
import java.util.List;
import java.util.Locale;

import com.ShoppingList.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

import java.util.ArrayList;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class ShowMap extends MapActivity {
    private MapView mapView;
    MapController mc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showmap);

        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapView.setClickable(true);

        Drawable marker=getResources().getDrawable(R.drawable.pushpin);
        marker.setBounds(0, 0, marker.getIntrinsicWidth(),
        marker.getIntrinsicHeight());

        InterestingLocations funPlaces = new InterestingLocations(marker);
        mapView.getOverlays().add(funPlaces);
        GeoPoint pt = funPlaces.getCenter(); // get the first-ranked point
        mapView.getController().setCenter(pt);
        mapView.getController().setZoom(15);

    }
    @Override
    protected boolean isLocationDisplayed() {
    return false;
    }
    @Override
    protected boolean isRouteDisplayed() {
    return false;
    }
    class InterestingLocations extends ItemizedOverlay {
        private List<OverlayItem> locations = new ArrayList<OverlayItem>();
        private Drawable marker;
        String adresa;
        public InterestingLocations(Drawable marker)
        {
        super(marker);
        this.marker=marker;
        // create locations of interest
        Bundle bundle = getIntent().getExtras();
        adresa = bundle.getString("adress");

        Geocoder geoCoder = new Geocoder(ShowMap.this, Locale.getDefault());
        try {
        List<Address> addresses = geoCoder.getFromLocationName(adresa, 5);
            String add = "";
            if (addresses.size() > 0) {
                GeoPoint p = new GeoPoint((int) (addresses.get(0).getLatitude() * 1E6), 
                        (int) (addresses.get(0).getLongitude() * 1E6));

                locations.add(new OverlayItem(p ,"Kaufland", "Magic Kingdom"));

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        populate();
    }
    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    super.draw(canvas, mapView, shadow);
    boundCenterBottom(marker);
    }
    @Override
    protected OverlayItem createItem(int i) {
    return locations.get(i);
    }
    @Override
    public int size() {
    return locations.size();
    }
    }
}

这是 LogCat:

06-03 18:36:17.082: ERROR/AndroidRuntime(2296): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ShoppingList/com.ShoppingList.Shops.ShowMap}: java.lang.NullPointerException
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at android.os.Looper.loop(Looper.java:123)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at android.app.ActivityThread.main(ActivityThread.java:4363)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at java.lang.reflect.Method.invokeNative(Native Method)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at java.lang.reflect.Method.invoke(Method.java:521)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at dalvik.system.NativeStart.main(Native Method)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296): Caused by: java.lang.NullPointerException
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at com.google.android.maps.MapController.setCenter(MapController.java:345)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at com.ShoppingList.Shops.ShowMap.onCreate(ShowMap.java:43)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
06-03 18:36:18.881: INFO/Process(2296): Sending signal. PID: 2296 SIG: 9

I have a strange problem. I want to display on map a certain location. The problem is that sometimes everything works fine, but sometimes I get force close and in DDMS I get :
06-03 18:24:14.171: ERROR/AndroidRuntime(2228): at com.google.android.maps.MapController.setCenter(MapController.java:345)
It seems that the problem is at line : mapView.getController().setCenter(pt). I can understand,why sometimes everything is fine,and sometimes is not ok.

Here is my code :

package com.ShoppingList.Shops;


import java.io.IOException;
import java.util.List;
import java.util.Locale;

import com.ShoppingList.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

import java.util.ArrayList;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class ShowMap extends MapActivity {
    private MapView mapView;
    MapController mc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showmap);

        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapView.setClickable(true);

        Drawable marker=getResources().getDrawable(R.drawable.pushpin);
        marker.setBounds(0, 0, marker.getIntrinsicWidth(),
        marker.getIntrinsicHeight());

        InterestingLocations funPlaces = new InterestingLocations(marker);
        mapView.getOverlays().add(funPlaces);
        GeoPoint pt = funPlaces.getCenter(); // get the first-ranked point
        mapView.getController().setCenter(pt);
        mapView.getController().setZoom(15);

    }
    @Override
    protected boolean isLocationDisplayed() {
    return false;
    }
    @Override
    protected boolean isRouteDisplayed() {
    return false;
    }
    class InterestingLocations extends ItemizedOverlay {
        private List<OverlayItem> locations = new ArrayList<OverlayItem>();
        private Drawable marker;
        String adresa;
        public InterestingLocations(Drawable marker)
        {
        super(marker);
        this.marker=marker;
        // create locations of interest
        Bundle bundle = getIntent().getExtras();
        adresa = bundle.getString("adress");

        Geocoder geoCoder = new Geocoder(ShowMap.this, Locale.getDefault());
        try {
        List<Address> addresses = geoCoder.getFromLocationName(adresa, 5);
            String add = "";
            if (addresses.size() > 0) {
                GeoPoint p = new GeoPoint((int) (addresses.get(0).getLatitude() * 1E6), 
                        (int) (addresses.get(0).getLongitude() * 1E6));

                locations.add(new OverlayItem(p ,"Kaufland", "Magic Kingdom"));

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        populate();
    }
    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    super.draw(canvas, mapView, shadow);
    boundCenterBottom(marker);
    }
    @Override
    protected OverlayItem createItem(int i) {
    return locations.get(i);
    }
    @Override
    public int size() {
    return locations.size();
    }
    }
}

Here is LogCat :

06-03 18:36:17.082: ERROR/AndroidRuntime(2296): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ShoppingList/com.ShoppingList.Shops.ShowMap}: java.lang.NullPointerException
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at android.os.Looper.loop(Looper.java:123)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at android.app.ActivityThread.main(ActivityThread.java:4363)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at java.lang.reflect.Method.invokeNative(Native Method)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at java.lang.reflect.Method.invoke(Method.java:521)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at dalvik.system.NativeStart.main(Native Method)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296): Caused by: java.lang.NullPointerException
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at com.google.android.maps.MapController.setCenter(MapController.java:345)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at com.ShoppingList.Shops.ShowMap.onCreate(ShowMap.java:43)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-03 18:36:17.082: ERROR/AndroidRuntime(2296):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
06-03 18:36:18.881: INFO/Process(2296): Sending signal. PID: 2296 SIG: 9

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽容 2024-11-20 10:34:34

您应该包含完整的日志跟踪,但可能 funPlaces.getCenter(); 有时不返回有效点?

You should include a full log trace, but could it be that funPlaces.getCenter(); sometimes does not return a valid point?

花期渐远 2024-11-20 10:34:34

尝试将任何 GeoPoint 调用包装在 try/catch 块中。不要以为它总是会给你带来价值。

Try wrapping any GeoPoint calls in a try/catch block. Don't assume it will always give you a value.

友欢 2024-11-20 10:34:34

我怀疑 pt 因错误而为 null

I suspect pt is null from the error

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文