Android 离线osm应用程序上的触摸事件方法(应用程序强制关闭)
这是一个用于离线osm地图的应用程序。应用程序在没有touchy类的情况下运行良好,但在添加Android 离线osm应用程序上的触摸事件方法(应用程序强制关闭),android,osmdroid,Android,Osmdroid,这是一个用于离线osm地图的应用程序。应用程序在没有touchy类的情况下运行良好,但在添加touchy()class后,应用程序将关闭 import java.util.List; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.graphics.C
touchy()
class后,应用程序将关闭
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
}
}
}
import java.util.List;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.view.MotionEvent;
公共类MainActivity扩展活动实现OpenStreetMapContributorConstants{
长启动,长停止;
语境t;
我的定位罗盘;
私有地图视图m_地图视图;
列表列表;
//默认地图缩放级别:
私有整数映射\默认\缩放=12;
//默认地图纬度:
私人双地图默认纬度=31.039109;
//默认地图经度:
私人双地图默认经度=31.381073;
私有org.osmdroid.views.MapController-mMapController;
int mIncr=10000;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//指定要使用的XML布局:
setContentView(R.layout.activity_main);
m_mapView=(mapView)findViewById(R.id.mapView);
List overlaylist=m_mapView.getOverlays();
touchy t=新touchy();
叠加列表。添加(t);
compass=新的MyLocationOverlay(MainActivity.this,m_mapView);
添加(指南针);
//设置mapView控制器:
m_mapView.SetBuilTinZoomControl(真);
m_mapView.setMultiTouchControls(真);
m_mapView.setClickable(真);
m_mapView.setUseDataConnection(错误);
m_mapView.getController().setZoom(映射默认缩放);
m_mapView.getController().setCenter(
新的地质点(地图默认纬度,地图默认经度);
m_mapView.setTileSource(TileSourceFactory.MAPNIK);
LocationManager LocationManager=(LocationManager)getSystemService(Context.LOCATION\u服务);
地质点gPt0=新地质点(31.039109,31.381073);
地质点gPt1=新的地质点(gPt0.getLatitudeE6()+mIncr,gPt0.getLongitudeE6());
地质点gPt2=新的地质点(gPt0.getLatitudeE6()+mIncr,gPt0.GetLongtudee6()+mIncr);
地质点gPt3=新的地质点(gPt0.getLatitudeE6(),gPt0.GetLongtudee6()+mIncr);
mMapController=m_mapView.getController();
mMapController.setCenter(gPt0);
PathOverlay myPath=新的PathOverlay(Color.RED,this);
myPath.addPoint(gPt0);
myPath.addPoint(gPt1);
myPath.addPoint(gPt2);
myPath.addPoint(gPt3);
myPath.addPoint(gPt0);
m_mapView.getOverlays().add(myPath);
}
@凌驾
受保护的void onPause(){
//TODO自动生成的方法存根
compass.disableCompass();
super.onPause();
}
@凌驾
受保护的void onResume(){
//TODO自动生成的方法存根
compass.getMyLocation();
compass.enableMyLocation();
compass.runOnFirstFix(新的Runnable(){
公开募捐{
//第一次:删除圆并绘制点。
//第二次:删除圆和点,并绘制一个新点。
m_mapView.getOverlays().clear();
m_mapView.getOverlays().add(指南针);
m_mapView.postInvalidate();
}
});
compass.enableCompus();
super.onResume();
}
类touchy扩展覆盖{
公众敏感(){
//TODO自动生成的构造函数存根
超级(t);
}
公共布尔onTouchEvent(运动事件e、地图视图m){
如果(e.getAction()==MotionEvent.ACTION\u DOWN){
start=e.getEventTime();
}如果(e.getAction()==MotionEvent.ACTION\u UP){
stop=e.getEventTime();
}如果(停止-启动>3000){
AlertDialog alert=新建AlertDialog.Builder(MainActivity.this.create();
警报。设置标题(“进入街道条件”);
alert.setMessage(“您必须输入条件”);
alert.setButton(“确定”,新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
//TODO自动生成的方法存根
}
});
alert.setButton2(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
//TODO自动生成的方法存根
}
});
alert.setButton3(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
//TODO自动生成的方法存根
}
});
alert.show();
返回true;
}
返回false;
}
@凌驾
受保护的空绘图(画布arg0、地图视图arg1、布尔值arg2){
//TODO自动生成的方法存根
}
}
}
03-11 14:46:24.775:D/TextLayoutCache(3156):使用调试级别:0-已启用调试:0
03-11 14:46:24.905:D/AndroidRuntime(3156):关闭虚拟机
03-11 14:46:24.905:W/dalvikvm(3156):threadid=1:线程退出时出现未捕获异常(组=0x2b542210)
03-11 14:46:24.905:E/AndroidRuntime(3156):致命异常:主
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