强制关闭mapController?

时间:2011-06-03 18:32:22

标签: android mapactivity

我有一个奇怪的问题。我想在地图上显示某个位置。问题是,有时候一切正常,但有时我会强行关闭,在DDMS中,我得到: 06-03 18:24:14.171:ERROR / 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

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我怀疑pt从错误

中为空
相关问题