我有一個奇怪的問題。我想在地圖上顯示某個位置。問題是,有時一切正常,但有時我得到的力量關閉,並在DDMS我得到: 06 - 03:24:14.171:錯誤/ AndroidRuntime(2228):在com.google.android.maps.MapController.setCenter (MapController.java:345) 看來問題出現在:mapView.getController()。setCenter(pt)。我可以理解,爲什麼有時一切都很好,有時並不好。強制關閉mapController?
這裏是我的代碼:
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