这里我编写了在mapview活动中获取lat和long值的代码,但是它被强制关闭是否有任何修改或其他方法来获取这些?
public class GpsoverlayActivity extends Activity {
/** Called when the activity is first created. */
MapView map;
MapController mc;
GeoPoint gp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
map=(MapView)findViewById(R.id.mapView);
mc=map.getController();
mc.setCenter(gp);
mc.setZoom(20);
}
protected boolean onTap(int i) {
OverlayItem item=getItem(i);
GeoPoint geo=item.getPoint();
Point pt=map.getProjection().toPixels(geo, null);
String message=String.format("Lat: %f | Lon: %f\nX: %d | Y %d",
geo.getLatitudeE6()/1000000.0,
geo.getLongitudeE6()/1000000.0,
pt.x, pt.y);
Toast.makeText(getApplicationContext(),
message,
Toast.LENGTH_LONG).show();
return(true);
}
private OverlayItem getItem(int i) {
// TODO Auto-generated method stub
return null;
}
}
这是我上面代码的LogCat ......
12-22 14:50:33.912: D/ddm-heap(834): Got feature list request
12-22 14:50:34.252: E/ActivityThread(834): Failed to find provider info for com.google.settings
12-22 14:50:34.271: E/ActivityThread(834): Failed to find provider info for com.google.settings
12-22 14:50:34.352: E/ActivityThread(834): Failed to find provider info for com.google.settings
12-22 14:50:34.802: D/AndroidRuntime(834): Shutting down VM
12-22 14:50:34.812: W/dalvikvm(834): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
12-22 14:50:34.812: E/AndroidRuntime(834): Uncaught handler: thread main exiting due to uncaught exception
12-22 14:50:34.841: E/AndroidRuntime(834): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emigostec/com.emigostec.GmapviewActivity}: java.lang.NullPointerException
12-22 14:50:34.841: E/AndroidRuntime(834): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
12-22 14:50:34.841: E/AndroidRuntime(834): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
12-22 14:50:34.841: E/AndroidRuntime(834): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
12-22 14:50:34.841: E/AndroidRuntime(834): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
12-22 14:50:34.841: E/AndroidRuntime(834): at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 14:50:34.841: E/AndroidRuntime(834): at android.os.Looper.loop(Looper.java:123)
12-22 14:50:34.841: E/AndroidRuntime(834): at android.app.ActivityThread.main(ActivityThread.java:4363)
12-22 14:50:34.841: E/AndroidRuntime(834): at java.lang.reflect.Method.invokeNative(Native Method)
12-22 14:50:34.841: E/AndroidRuntime(834): at java.lang.reflect.Method.invoke(Method.java:521)
12-22 14:50:34.841: E/AndroidRuntime(834): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-22 14:50:34.841: E/AndroidRuntime(834): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-22 14:50:34.841: E/AndroidRuntime(834): at dalvik.system.NativeStart.main(Native Method)
12-22 14:50:34.841: E/AndroidRuntime(834): Caused by: java.lang.NullPointerException
12-22 14:50:34.841: E/AndroidRuntime(834): at com.emigostec.GmapviewActivity.onCreate(GmapviewActivity.java:24)
12-22 14:50:34.841: E/AndroidRuntime(834): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-22 14:50:34.841: E/AndroidRuntime(834): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
12-22 14:50:34.841: E/AndroidRuntime(834): ... 11 more
12-22 14:50:34.872: I/dalvikvm(834): threadid=7: reacting to signal 3
12-22 14:50:35.123: W/dalvikvm(834): threadid=7: spin on suspend #0 threadid=17 (h=1283560)
12-22 14:50:35.123: W/dalvikvm(834): dumping state: process - 834
12-22 14:50:35.123: I/dalvikvm(834): "Signal Catcher" daemon prio=5 tid=7 RUNNABLE
12-22 14:50:35.123: I/dalvikvm(834): | group="system" sCount=0 dsCount=0 s=N obj=0x44bf51e8 self=0x133828
12-22 14:50:35.123: I/dalvikvm(834): | sysTid=837 nice=0 sched=0/0 cgrp=default handle=1238520
12-22 14:50:35.123: I/dalvikvm(834): at dalvik.system.NativeStart.run(Native Method)
12-22 14:50:35.123: I/dalvikvm(834): "TrafficService" prio=1 tid=17 RUNNABLE
12-22 14:50:35.123: I/dalvikvm(834): | group="main" sCount=1 dsCount=0 s=N obj=0x44c184b8 self=0x11ec98
12-22 14:50:35.123: I/dalvikvm(834): | sysTid=842 nice=19 sched=0/0 cgrp=bg_non_interactive handle=1283560
12-22 14:50:35.123: I/dalvikvm(834): DumpRunning: Thread at 0x11ec98 has no curFrame (threadid=17)
12-22 14:50:35.162: W/dalvikvm(834): threadid=7: spin on suspend resolved in 293 msec
12-22 14:50:35.321: I/dalvikvm(834): Wrote stack trace to '/data/anr/traces.txt'
答案 0 :(得分:0)
我不知道你到底想要做什么,但我可以看到一些错误:
公共类GpsoverlayActivity扩展了MapActivity
关于gp,你没有在其中加一个值,插入任何纬度和经度如下:
GeoPoint gp= new GeoPoint((int)(26.2*1.0E6), (int)(52.6*1.0E6));
如果你想在地图上做标记,我建议你创建另一个类扩展ItemizedOverlay,就像这样的结构:
public class MapViewItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public MapViewItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
mContext = context;
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
public boolean onTap(int index)
{
//get the marker on the map from here
OverlayItem item = mOverlays.get(index);
OverlayItem item=getItem(i);
GeoPoint geo=item.getPoint();
Point pt=map.getProjection().toPixels(geo, null);
String message=String.format("Lat: %f | Lon: %f\nX: %d | Y %d",
geo.getLatitudeE6()/1000000.0,
geo.getLongitudeE6()/1000000.0,
pt.x, pt.y);
Toast.makeText(getApplicationContext(),
message,
Toast.LENGTH_LONG).show();
return true;
}
并在MapActivity中使用它:
MapView mapView = (MapView) findViewById(R.id.mapview);
MapController mc = mapView.getController();
List<Overlay> mapOverlays = mapView.getOverlays();
//add any icon here for the marker
Drawable drawable = MainActivity.this.getResources().getDrawable(R.drawable.icon);
MapViewItemizedOverlay itemizedOverlay = new MapViewItemizedOverlay(drawable,this);
//insert any lat and lng as integers here
GeoPoint point1 = new GeoPoint(lat,lng);
OverlayItem overlayitem1 = new OverlayItem(point1, "Info", "You are here!" );
itemizedOverlay.addOverlay(overlayitem1);
mapOverlays.add(itemizedOverlay);
mc.animateTo(point1);