开发者社区> 问答> 正文

android在子线程里读写文件里的经纬度并绘制轨迹,activity销毁时程序崩溃

public class DrawPathActivity extends BaseActivity {
private MapView mMapView;
private BaiduMap mBaiduMap;
private static final int POINT_COUNT_PER_GROUP = 100;

private LatLng startPoint;

private Handler handler;
private HandlerThread thread;
private Runnable runnable;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.draw_path_activity);
    mMapView = (MapView) this.findViewById(R.id.bmapView);
    mBaiduMap = mMapView.getMap();

    thread = new HandlerThread("drawPath");
    thread.start();
    handler = new Handler(thread.getLooper());
    runnable = new Runnable() {
        @Override
        public void run() {
                drawPath();
        }
    };
    handler.post(runnable);
}

/**
 * 绘制轨迹
 */
private void drawPath() {
    FileReader fileReader = null;
    BufferedReader bufferedReader = null;
    ArrayList<LatLng> pointsList = new ArrayList<LatLng>();
    String latlngString = null;
    try {
        fileReader = new FileReader(SystemConstant.FILE_ROOT_DIR + File.separator + SystemConstant.POINT_FILE_NAME);
        bufferedReader = new BufferedReader(fileReader);

        while((latlngString = bufferedReader.readLine()) != null) {
            String[] latlngArray = null;
            latlngArray = latlngString.split(",");
            double lat, lng;
            lat = StringConversionUtil.parseDouble(latlngArray[0], 0);
            lng = StringConversionUtil.parseDouble(latlngArray[1], 0);
            LatLng point = new LatLng(lat, lng);

            if(startPoint == null) {
                startPoint = point;
            }
            //更新地图坐标
            MapStatusUpdate u1 = MapStatusUpdateFactory.newLatLng(startPoint);
            mBaiduMap.setMapStatus(u1);
            //画点
            OverlayOptions ooDot = new DotOptions().center(point).radius(8).color(0xFF0000FF);
            mBaiduMap.addOverlay(ooDot);

            pointsList.add(point);
            //默认100个点为一组,画一次
            if(pointsList.size() >= POINT_COUNT_PER_GROUP) {
                //画线
                OverlayOptions ooPolyline = new PolylineOptions().width(6).color(0xAAFF0000).points(pointsList);
                mBaiduMap.addOverlay(ooPolyline);
                pointsList.clear();
                pointsList.add(point);
            }
        }
        //如果循环结束后,pointsList的个数小于100,直接将剩下的所有点连线
        OverlayOptions ooPolyline = new PolylineOptions().width(6).color(0xAAFF0000).points(pointsList);
        mBaiduMap.addOverlay(ooPolyline);
        pointsList.clear();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            if(bufferedReader != null) {
                bufferedReader.close();
            }
            if(fileReader != null) {
                fileReader.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

@Override
protected void onPause() {
    mMapView.onPause();
    super.onPause();
}

@Override
protected void onResume() {
    mMapView.onResume();
    super.onResume();
}

@Override
protected void onDestroy() {

    if (handler != null) {
        handler.removeCallbacks(runnable);
    }
    mMapView.onDestroy();
    super.onDestroy();
}


}

我是在子线程里调用drawPath方法,这个方法主要就是从文件中读出经纬度然后在百度地图上画轨迹,每次执行的时候,如果轨迹没有完全画完就退出,程序就会崩溃,如果一直等待画完再退出,就不会崩溃

展开
收起
爵霸 2016-06-01 13:33:22 2895 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载