我在我的应用程序中找到了地点列表,并且我已将纬度和经度传递给另一个类以在地图上显示该位置,但在单击列出的地点后显示“强制关闭应用程序”。
代码是:
使用 onClick 列出地点
ListView lv = getListView();
// listening to single list item click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String lat = ((TextView) view.findViewById(R.id.lat)).getText().toString();
String lng = ((TextView) view.findViewById(R.id.lng)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), ViewOnMap.class);
in.putExtra(KEY_LAT, lat);
in.putExtra(KEY_LONG, lng);
startActivity(in);
}
});
然后我创建了另一个类来显示地图上的位置:
public class ViewOnMap extends MapActivity {
static final String KEY_LAT = "lat";
static final String KEY_LONG = "lng";
MapView mview;
MapController mcontrol;
LocationManager locmanager;
Location location;
GeoPoint geopoint;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
Intent in = getIntent();
List<Overlay> mapOverlays = mview.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
MapOverlay itemizedoverlay = new MapOverlay(drawable, this);
String lat = in.getStringExtra(KEY_LAT);
String lng = in.getStringExtra(KEY_LONG);
int latitude = Integer.parseInt(lat);
int longitude = Integer.parseInt(lng);
mview =(MapView)findViewById(R.id.mapv);
mview.displayZoomControls(true);
mview.setBuiltInZoomControls(true);
mcontrol =mview.getController();
mcontrol.setZoom(9);
GeoPoint point = new GeoPoint(latitude,longitude);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
请告诉我是什么错误。这些是 LogCat 中的错误:
04-17 18:07:54.509: E/AndroidRuntime(13783): 致命异常: main 04-17 18:07:54.509: E/AndroidRuntime(13783): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com. run.trial/com.run.trial.ViewOnMap}:java.lang.NullPointerException 04-17 18:07:54.509:E/AndroidRuntime(13783):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768) 04 -17 18:07:54.509: E/AndroidRuntime(13783): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784) 04-17 18:07:54.509: E/AndroidRuntime(13783): 在 android.app .ActivityThread.access$1500(ActivityThread.java:123) 04-17 18:07:54.509: E/AndroidRuntime(13783): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:939) 04-17 18: 07:54.509: E/AndroidRuntime(13783): 在 android.os.Handler.dispatchMessage(Handler.java:99) 04-17 18:07:54.509: E/AndroidRuntime(13783): 在 android.os.Looper.loop(Looper.java:130) 04-17 18:07:54.509: E/AndroidRuntime(13783): 在android.app.ActivityThread.main(ActivityThread.java:3835)