我有一段NullPointerException
时间使用 MapActvity
异常结果是:
05-19 15:34:23.345: E/AndroidRuntime(3126): FATAL EXCEPTION: main
05-19 15:34:23.345: E/AndroidRuntime(3126): java.lang.NullPointerException
05-19 15:34:23.345: E/AndroidRuntime(3126): at com.google.android.maps.ItemizedOverlay.getIndexToDraw(ItemizedOverlay.java:211)
05-19 15:34:23.345: E/AndroidRuntime(3126): at com.google.android.maps.ItemizedOverlay.draw(ItemizedOverlay.java:240)
05-19 15:34:23.345: E/AndroidRuntime(3126): at com.google.android.maps.Overlay.draw(Overlay.java:179)
05-19 15:34:23.345: E/AndroidRuntime(3126): at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:42)
05-19 15:34:23.345: E/AndroidRuntime(3126): at com.google.android.maps.MapView.onDraw(MapView.java:530)
05-19 15:34:23.345: E/AndroidRuntime(3126): at android.view.View.draw(View.java:6880)
05-19 15:34:23.345: E/AndroidRuntime(3126): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
05-19 15:34:23.345: E/AndroidRuntime(3126): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
05-19 15:34:23.345: E/AndroidRuntime(3126): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
05-19 15:34:23.345: E/AndroidRuntime(3126): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
05-19 15:34:23.345: E/AndroidRuntime(3126): at android.view.View.draw(View.java:6883)
05-19 15:34:23.345: E/AndroidRuntime(3126): at android.widget.FrameLayout.draw(FrameLayout.java:357)
05-19 15:34:23.345: E/AndroidRuntime(3126): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
05-19 15:34:23.345: E/AndroidRuntime(3126): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
05-19 15:34:23.345: E/AndroidRuntime(3126): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
05-19 15:34:23.345: E/AndroidRuntime(3126): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
05-19 15:34:23.345: E/AndroidRuntime(3126): at android.view.View.draw(View.java:6883)
05-19 15:34:23.345: E/AndroidRuntime(3126): at android.widget.FrameLayout.draw(FrameLayout.java:357)
05-19 15:34:23.345: E/AndroidRuntime(3126): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
05-19 15:34:23.345: E/AndroidRuntime(3126): at android.view.ViewRoot.draw(ViewRoot.java:1522)
05-19 15:34:23.345: E/AndroidRuntime(3126): at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
05-19 15:34:23.345: E/AndroidRuntime(3126): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
05-19 15:34:23.345: E/AndroidRuntime(3126): at android.os.Handler.dispatchMessage(Handler.java:99)
05-19 15:34:23.345: E/AndroidRuntime(3126): at android.os.Looper.loop(Looper.java:130)
05-19 15:34:23.345: E/AndroidRuntime(3126): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-19 15:34:23.345: E/AndroidRuntime(3126): at java.lang.reflect.Method.invokeNative(Native Method)
05-19 15:34:23.345: E/AndroidRuntime(3126): at java.lang.reflect.Method.invoke(Method.java:507)
05-19 15:34:23.345: E/AndroidRuntime(3126): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-19 15:34:23.345: E/AndroidRuntime(3126): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-19 15:34:23.345: E/AndroidRuntime(3126): at dalvik.system.NativeStart.main(Native Method)
MapActivity 的代码是:
package pit.opensource.mapapp;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
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 com.google.android.maps.MyLocationOverlay;
public class M1MapAppActivity extends MapActivity {
private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
private MyOverlays itemizedoverlay;
private MyLocationOverlay myLocationOverlay;
private GeoPoint myLocation;
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main); // bind the layout to the activity
// Configure the Map
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(true);
mapController = mapView.getController();
mapController.setZoom(1); // Zoon 1 is world view
Criteria c = new Criteria();
//c.setAccuracy(Criteria)
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0, new GeoUpdateHandler());
//myLocationOverlay = new MyLocationOverlay(this, mapView);
//mapView.getOverlays().add(myLocationOverlay);
/*myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
myLocation = myLocationOverlay.getMyLocation();
}
});*/
Drawable drawable = this.getResources().getDrawable(R.drawable.point);
itemizedoverlay = new MyOverlays(this, drawable);
createMarker();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
public class GeoUpdateHandler implements LocationListener {
@Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(lat, lng);
// createMarker();
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
private void createMarker() {
Log.e("Create Marker","-------------");
// GeoPoint p = mapView.getMapCenter();
// OverlayItem overlayitem = new OverlayItem(p,"Title: Abu Said Resturant","Best Falafel in Gaza :) ");
Place p1 = new Place();
p1.point = new GeoPoint(345455545, 454558545);
p1.Title = "PIT";
p1.Description = "Open source course";
mapController.animateTo(p1.point); // mapController.setCenter(point);
itemizedoverlay.addOverlayItem(p1);
Log.d("Itemized overlay=========",itemizedoverlay.size()+"");
if (itemizedoverlay.size() > 0) {
mapView.getOverlays().add(itemizedoverlay);
}
}
@Override
protected void onResume() {
super.onResume();
// myLocationOverlay.enableMyLocation();
// myLocationOverlay.enableCompass();
}
@Override
protected void onPause() {
super.onResume();
// myLocationOverlay.disableMyLocation();
// myLocationOverlay.disableCompass();
}
}
以下是 MyOverlay 的代码:
package pit.opensource.mapapp;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.widget.Toast;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class MyOverlays extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> overlays= new ArrayList<OverlayItem>();
private Context context;
public MyOverlays(Context context, Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
this.context = context;
}
@Override
protected OverlayItem createItem(int i) {
Log.d("*******createItem",overlays.get(i).getTitle());
return overlays.get(i);
}
@Override
public int size() {
Log.d("Item size ", overlays.size()+"");
return overlays.size();
}
public void addOverlayItem(Place p) {
Log.d("Overlay item ------ ",p.Title);
OverlayItem o = new OverlayItem(p.point, p.Title, p.Description);
overlays.add(o);
}
protected boolean onTap(int index) {
OverlayItem overlayItem = overlays.get(index);
String title = overlayItem.getTitle();
Toast.makeText(context, title, Toast.LENGTH_LONG).show();
return true;
};
}