致命异常返回菜单

时间:2014-06-06 14:38:19

标签: java android

所以我做了一点,不是什么大不了,只是学习图形,但是当我点击后退按钮时,应用程序崩溃了,它给了我致命的错误11。

代码:

public class MyPachuSurface extends SurfaceView implements Runnable{

    SurfaceHolder myHolder;
    Thread myThread = null;
    boolean isRunning = false;

    public float x,y,startX,startY,finalX,finalY,dx,dy,tx,ty;

    public MyPachuSurface(Context context) 
    {
        super(context);
        // TODO Auto-generated constructor stub
        myHolder = getHolder();
        this.x = 0;
        this.y = 0;
        this.startX = 0;
        this.startY = 0;
        this.finalX = 0;
        this.finalY = 0;
        this.dx = 0;
        this.dy = 0;
        this.tx = 0;
        this.ty = 0;
    }

    public void pause()
    {
        isRunning = false;

        try 
        {
            myThread.join();
        } 
        catch (InterruptedException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        myThread = null;
    }

    public void resume ()
    {
        isRunning = true;
        myThread = new Thread(this);
        myThread.start();
    }

    @Override
    public void run() 
    {
        // TODO Auto-generated method stub
        while(isRunning)
        {
            if(!myHolder.getSurface().isValid())
                continue;

            Canvas canvas = myHolder.lockCanvas();
            canvas.drawRGB(02, 02, 254);
            if(this.x != 0 && this.y != 0)
            {
                Bitmap test = BitmapFactory.decodeResource(getResources(), R.drawable.plus1);
                canvas.drawBitmap(test, this.x - test.getWidth()/2 - this.tx, this.y - test.getHeight()/2 - this.ty, null);
                this.tx += this.dx/30;
                this.ty += this.dy/30;
            }
            myHolder.unlockCanvasAndPost(canvas);
        }
    }

}

这是视图,这是活动:

public class MyGraphicsSurface extends Activity implements OnTouchListener{

    MyPachuSurface pachuSurf;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        pachuSurf = new MyPachuSurface(this);
        pachuSurf.setOnTouchListener(this);
        setContentView(pachuSurf);
    }
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        pachuSurf.resume();
    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        pachuSurf.resume();
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) 
    {
        // TODO Auto-generated method stub
        this.pachuSurf.x = event.getX();
        this.pachuSurf.y = event.getY();

        switch(event.getAction())
        {
        case MotionEvent.ACTION_DOWN:
            pachuSurf.startX = event.getX();
            pachuSurf.startY = event.getY();
            pachuSurf.tx = 0;
            pachuSurf.ty = 0;
            break;
        case MotionEvent.ACTION_UP:
            pachuSurf.finalX = event.getX();
            pachuSurf.finalY = event.getY();
            pachuSurf.dx = pachuSurf.finalX - pachuSurf.startX;
            pachuSurf.dy = pachuSurf.finalY - pachuSurf.startY;
            break;
        }
        return true;
    }

}

这是Loc Cat:

06-06 17:37:20.104: E/AndroidRuntime(22781): FATAL EXCEPTION: Thread-29865
06-06 17:37:20.104: E/AndroidRuntime(22781): java.lang.NullPointerException
06-06 17:37:20.104: E/AndroidRuntime(22781):    at com.example.myfirstapp.MyPachuSurface.run(MyPachuSurface.java:68)
06-06 17:37:20.104: E/AndroidRuntime(22781):    at java.lang.Thread.run(Thread.java:856)

0 个答案:

没有答案