这是一个离线osm地图的应用程序。该应用程序在没有敏感课程的情况下运行良好,但在添加touchy()
课程后应用程序关闭
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.Color;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.MotionEvent;
public class MainActivity extends Activity implements OpenStreetMapContributorConstants{
long start,stop;
Context t;
MyLocationOverlay compass;
private MapView m_mapView;
List overlaylist ;
// Default map zoom level:
private int MAP_DEFAULT_ZOOM = 12;
// Default map Latitude:
private double MAP_DEFAULT_LATITUDE = 31.039109;
// Default map Longitude:
private double MAP_DEFAULT_LONGITUDE = 31.381073;
private org.osmdroid.views.MapController mMapController;
int mIncr = 10000;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Specify the XML layout to use:
setContentView(R.layout.activity_main);
m_mapView =(MapView) findViewById(R.id.mapview);
List<Overlay> overlaylist = m_mapView.getOverlays();
touchy t =new touchy();
overlaylist.add(t);
compass =new MyLocationOverlay(MainActivity.this,m_mapView);
overlaylist.add(compass);
// Setup the mapView controller:
m_mapView.setBuiltInZoomControls(true);
m_mapView.setMultiTouchControls(true);
m_mapView.setClickable(true);
m_mapView.setUseDataConnection(false);
m_mapView.getController().setZoom(MAP_DEFAULT_ZOOM);
m_mapView.getController().setCenter(
new GeoPoint(MAP_DEFAULT_LATITUDE, MAP_DEFAULT_LONGITUDE));
m_mapView.setTileSource(TileSourceFactory.MAPNIK);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
GeoPoint gPt0 = new GeoPoint(31.039109, 31.381073);
GeoPoint gPt1 = new GeoPoint(gPt0.getLatitudeE6()+ mIncr, gPt0.getLongitudeE6());
GeoPoint gPt2 = new GeoPoint(gPt0.getLatitudeE6()+ mIncr, gPt0.getLongitudeE6() + mIncr);
GeoPoint gPt3 = new GeoPoint(gPt0.getLatitudeE6(), gPt0.getLongitudeE6() + mIncr);
mMapController = m_mapView.getController();
mMapController.setCenter(gPt0);
PathOverlay myPath = new PathOverlay(Color.RED, this);
myPath.addPoint(gPt0);
myPath.addPoint(gPt1);
myPath.addPoint(gPt2);
myPath.addPoint(gPt3);
myPath.addPoint(gPt0);
m_mapView.getOverlays().add(myPath);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
compass.disableCompass();
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
compass.getMyLocation();
compass.enableMyLocation();
compass.runOnFirstFix(new Runnable(){
public void run() {
// First time: removes the circle and draws a dot.
// Second time: removes the circle and dot, and draw a new dot.
m_mapView.getOverlays().clear();
m_mapView.getOverlays().add(compass);
m_mapView.postInvalidate();
}
});
compass.enableCompass();
super.onResume();
}
class touchy extends Overlay{
public touchy() {
// TODO Auto-generated constructor stub
super(t);
}
public boolean onTouchEvent(MotionEvent e,MapView m){
if(e.getAction() ==MotionEvent.ACTION_DOWN){
start = e.getEventTime();
}if(e.getAction() ==MotionEvent.ACTION_UP){
stop = e.getEventTime();
}if(stop - start >3000){
AlertDialog alert =new AlertDialog.Builder(MainActivity.this).create();
alert.setTitle("Enter the street Condition");
alert.setMessage("you must enter the condition ");
alert.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alert.setButton2("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alert.setButton3("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alert.show();
return true;
}
return false;
}
@Override
protected void draw(Canvas arg0, MapView arg1, boolean arg2) {
// TODO Auto-generated method stub
}
}
}
03-11 14:46:24.775:D/TextLayoutCache(3156):使用调试级别:0 - 启用调试:0
03-11 14:46:24.905: D/AndroidRuntime(3156): 关闭 VM 03-11 14:46:24.905: W/dalvikvm(3156): threadid=1: 线程退出未捕获异常 (group=0x2b542210) 03 -11 14:46:24.905: E/AndroidRuntime(3156): 致命异常: main 03-11 14:46:24.905: E/AndroidRuntime(3156): java.lang.NullPointerException 03-11 14:46:24.905: E /AndroidRuntime(3156): 在 org.osmdroid.views.overlay.OverlayManager.onDraw(OverlayManager.java:107) 03-11 14:46:24.905: E/AndroidRuntime(3156): 在 org.osmdroid.views.MapView。 dispatchDraw(MapView.java:818) 03-11 14:46:24.905: E/AndroidRuntime(3156): at android.view.ViewGroup.drawChild(ViewGroup.java:2989) 03-11 14:46:24.905: E/ AndroidRuntime(3156): 在 android.view.ViewGroup.dispatchDraw(ViewGroup.java:2593) 03-11 14:46:24.905: E/AndroidRuntime(3156): 在 android.view.ViewGroup.drawChild(ViewGroup.java:2989) 03-11 14:46:24.905: E/AndroidRuntime(3156): 在 android.view.ViewGroup.dispatchDraw(ViewGroup.java:2593) 03-11 14:46:24.905: E/AndroidRuntime(3156): 在android.view.ViewGroup.drawChild(ViewGroup.java:2989) 03-11 14:46:24.905: E/AndroidRuntime(3156): 在 android.view.ViewGroup.dispatchDraw(ViewGroup.java:2593) 03-11 14: 46:24.905: E/AndroidRuntime(3156): 在 android.view.ViewGroup.drawChild(ViewGroup.java:2989) 03-11 14:46:24.905: E/AndroidRuntime(3156): 在 android.view.ViewGroup.dispatchDraw (ViewGroup.java:2593) 03-11 14:46:24.905: E/AndroidRuntime(3156): 在 android.view.View.draw(View.java:11085) 03-11 14:46:24.905: E/AndroidRuntime (3156): 在 android.widget.FrameLayout.draw(FrameLayout.java:462) 03-11 14:46:24.905: E/AndroidRuntime(3156): 在 com.android.internal.policy.impl.PhoneWindow$DecorView。绘制(电话窗口。java:2145) 03-11 14:46:24.905: E/AndroidRuntime(3156): 在 android.view.ViewRootImpl.draw(ViewRootImpl.java:2026) 03-11 14:46:24.905: E/AndroidRuntime(3156) : 在 android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1634) 03-11 14:46:24.905: E/AndroidRuntime(3156): 在 android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442) 03-11 14:46:24.905: E/AndroidRuntime(3156): 在 android.os.Handler.dispatchMessage(Handler.java:99) 03-11 14:46:24.905: E/AndroidRuntime(3156): 在 android.os.Looper .loop(Looper.java:137) 03-11 14:46:24.905: E/AndroidRuntime(3156): 在 android.app.ActivityThread.main(ActivityThread.java:4441) 03-11 14:46:24.905: E /AndroidRuntime(3156): at java.lang.reflect.Method.invokeNative(Native Method) 03-11 14:46:24.905: E/AndroidRuntime(3156): at java.lang.reflect.Method.invoke(Method.java :511) 03-11 14:46:24.905: E/AndroidRuntime(3156): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 03-11 14:46:24.905: E/ AndroidRuntime(3156): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 03-11 14:46:24.905: E/AndroidRuntime(3156): 在 dalvik.system.NativeStart.main(Native方法) ##