问题
Here I wrote code for getting lat and long values in my mapview activity, but its getting forced closed is there any modification or other methods to get these?
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;
}
}
here this is my LogCat for above code...
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'
回答1:
I don't know what exactly you are trying to do here, but I can see some errors:
- class declaration is wrong (you have to use MapActivity not Activity):
public class GpsoverlayActivity extends MapActivity
about gp, you didn't put a value in it, insert any latitude and longitude like this:
GeoPoint gp= new GeoPoint((int)(26.2*1.0E6), (int)(52.6*1.0E6));
if you are trying to marker on the map I suggest you create another class extends ItemizedOverlay like this structure:
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;
}
and use it like this in the 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);
来源:https://stackoverflow.com/questions/8601329/i-didnt-get-my-latitude-and-longitude-in-my-mapview