Android 调试时出现旋转贴图视图错误

Android 调试时出现旋转贴图视图错误,android,map,orientation,android-mapview,rotation,Android,Map,Orientation,Android Mapview,Rotation,我开发了一个可以旋转方向视图的地图,这取决于用户面对的位置(例如,如果用户转向南方,则地图将旋转到南方方向,如指南针)。我已经尝试了这个项目几周,但收效甚微。我在eclipse上没有错误消息,但是当我调试代码时,应用程序总是崩溃。 谢谢,我真的需要所有的帮助 private class RotateView extends ViewGroup { private Matrix mMatrix = new Matrix(); private float[] mTemp

我开发了一个可以旋转方向视图的地图,这取决于用户面对的位置(例如,如果用户转向南方,则地图将旋转到南方方向,如指南针)。我已经尝试了这个项目几周,但收效甚微。我在eclipse上没有错误消息,但是当我调试代码时,应用程序总是崩溃。 谢谢,我真的需要所有的帮助

    private class RotateView extends ViewGroup  {

    private Matrix mMatrix = new Matrix();
    private float[] mTemp = new float [2];
    private static final float SQ2 = 1.414213562373095f;


    public RotateView(Context context) {
        super(context);

        // TODO Auto-generated constructor stub
    }



    @Override
    protected void dispatchDraw(Canvas canvas) {
        // TODO Auto-generated method stub

        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.rotate(-mHeading,getWidth()*0.5f , getHeight()*0.5f);
        canvas.getMatrix(mMatrix);
        super.dispatchDraw(canvas);
        canvas.restore();
    }



    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub
        final int width = getWidth();
        final int height = getHeight();
        final int count = getChildCount();
        for (int i=0; i<=count; i++){
            final View view = getChildAt(i);
            final int childWidth = view.getMeasuredWidth();
            final int childHeight = view.getMeasuredHeight();
            final int childLeft = ((width - childWidth)/2);
            final int childTop = ((height-childHeight)/2);
            view.layout(childLeft, childTop, childLeft+childWidth, childTop+childHeight); 
        }
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        int mW = getDefaultSize(getMeasuredWidth(), widthMeasureSpec);
        int mH = getDefaultSize(getMeasuredHeight(), heightMeasureSpec);
        int sizeSpec;
        if(mW > mH){
            sizeSpec = MeasureSpec.makeMeasureSpec((int)(mW*SQ2), MeasureSpec.EXACTLY);
        } else {
            sizeSpec = MeasureSpec.makeMeasureSpec((int) (mH*SQ2), MeasureSpec.EXACTLY);
        }
        final int count = getChildCount();
        for(int i = 0; i<count; i++){
            getChildAt(i).measure(sizeSpec, sizeSpec);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent e) {
        // TODO Auto-generated method stub

        return super.dispatchTouchEvent(e);
    }

}
// End of class RotateView

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 

    sensormanager =(SensorManager)getSystemService(SENSOR_SERVICE);
    rotateview = new RotateView(this);
    map = new MapView (this,"Map Key");
    rotateview.addView(map);
    setContentView(rotateview);
    controll();
}

public void controll () {

        controller = map.getController();
        //Zoom
        map.setBuiltInZoomControls(true);
        controller.setZoom(19);
        overlayList = map.getOverlays();
        map.setClickable(true);
        map.setEnabled(true);
        //compass
        compass = new MyLocationOverlay(MapIpbActivity.this, map);
        overlayList.add(compass);
        myLocation();
        Touchy t = new Touchy();
        overlayList.add(t);
}

public void myLocation() {
    ourLocation = new MyLocationOverlay(MapIpbActivity.this, map);
    map.getOverlays().add(ourLocation);
    ourLocation.runOnFirstFix(new Runnable() {
        public void run(){
            controller.animateTo(ourLocation.getMyLocation());
        }

    });
    }



@Override
protected void onPause() {
    // TODO Auto-generated method stub
    compass.disableCompass();
    ourLocation.disableMyLocation();
    sensormanager.unregisterListener(orientListener);
    super.onPause();
}
@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    compass.enableCompass();
    ourLocation.enableMyLocation();
    sensormanager.registerListener(orientListener, 
            sensormanager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
            sensormanager.SENSOR_DELAY_NORMAL);

}

final SensorEventListener orientListener = new SensorEventListener() {

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        if(event.sensor.getType() == Sensor.TYPE_ORIENTATION){
            mHeading = event.values[0];
            rotateview.invalidate();
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }
};
    class Touchy extends Overlay {
        long start;
        long stop;
        int x;
        int y;
        GeoPoint TouchedPoint;

        @Override
        public boolean onTouchEvent(MotionEvent e, MapView m) {
            // TODO Auto-generated method stub
            if(e.getAction() == MotionEvent.ACTION_DOWN) {
                start = e.getEventTime();
                x = (int) e.getX();
                y = (int) e.getY();
                TouchedPoint = map.getProjection().fromPixels(x, y);
            }
            if (e.getAction() == MotionEvent.ACTION_UP) {
                stop = e.getEventTime();
            }if (stop - start > 2000){
                    box();
                    return true;
            }
            return false;
        }
私有类RotateView扩展了ViewGroup{
私有矩阵mMatrix=新矩阵();
私有浮动[]mTemp=新浮动[2];
专用静态最终浮动SQ2=1.414213562373095f;
公共旋转视图(上下文){
超级(上下文);
//TODO自动生成的构造函数存根
}
@凌驾
受保护的void dispatchDraw(画布){
//TODO自动生成的方法存根
canvas.save(canvas.MATRIX\u save\u标志);
画布旋转(-mHeading,getWidth()*0.5f,getHeight()*0.5f);
canvas.getMatrix(mMatrix);
super.dispatchDraw(画布);
canvas.restore();
}
@凌驾
仅受保护的void布局(布尔值已更改、int l、int t、int r、int b){
//TODO自动生成的方法存根
最终整数宽度=getWidth();
最终整数高度=getHeight();
最终整数计数=getChildCount();
对于(int i=0;i mH){
sizeSpec=MeasureSpec.makeMeasureSpec((int)(mW*SQ2),MeasureSpec.justice);
}否则{
sizeSpec=MeasureSpec.makeMeasureSpec((int)(mH*SQ2),MeasureSpec.justice);
}
最终整数计数=getChildCount();
对于(int i=0;i 2000){
box();
返回true;
}
返回false;
}

解决了这个问题,只需更改
解决了这个问题,只需更改i