机器人错误。 (新手编码器)

时间:2011-04-02 23:58:44

标签: android nullpointerexception

好的,所以我刚开始尝试学习如何在Android平台上使用Eclipse进行一些编码。我编写了很多其他语言,但从来没有在这种环境中。我不知道为什么会这样......

症状:App无法在模拟器中启动。

Debugger Error: "Thread [<8> Thread-8] (Suspended (exception NullPointerException))" TutorialThread.run() line: 225

此行是结束括号。

Variables: 
this (TutorialThread (id=30067699104))
c (Surface$CompaitbleCanvas (id-830067697136))

就像我说的那样,我对此非常陌生,而且我一直在关注大量的教程和大量的Google搜索。 :/但是,我现在正在敲打键盘。非常感谢任何帮助。

package com.Joey_Ant.Lite;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;

public class JoeyAnt extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(new Panel(this));
    }
}
class Panel extends SurfaceView implements SurfaceHolder.Callback {
    private TutorialThread _thread;
    private int _x=20;
    private int _y=20;
    private int _x2=20;
    private int _y2=20;
    private int _mx=0;
    private int _my=0;
    private double _angle = 0;
    private int _width=50;
    private int _height=50;
    private double _xchange=0;
    private double _ychange=0;
    private double _speed=5;
    private int _stage=0;
    private int _level=0;
    private int _swidth=0;
    private int _sheight=0;
    private int _bx=0;
    private int _b0w=0;
    private int _b0h=0;
    private int _b0y=0;
    private int _b1w=0;
    private int _b1h=0;
    private int _b1y=0;
    private int _b2w=0;
    private int _b2h=0;
    private int _b2y=0;
    private int _b3w=0;
    private int _b3h=0;
    private int _b3y=0;

    /** 
     * Stage 0 - Main Menu
     * Stage 1 - Controls
     * Stage 2 - Introduction
     * Stage 3 - Game Over
     * Stage 4 - Round Win
     * Stage 5 - In Game
     * 
     */

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        _thread.setRunning(true);
        _thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        _thread.setRunning(false);
        while (retry) {
            try {
                _thread.join();
                retry=false;
            } catch (InterruptedException e) {
                //Retry
            }
        }
    }

    public Panel(Context context) {
        super(context);
        getHolder().addCallback(this);
        _thread = new TutorialThread(getHolder(), this);
        setFocusable(true);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        _mx = (int) event.getX();
        _my = (int) event.getY();
        _x2 = _mx - (_width / 2);
        _y2 = _my - (_height / 2);

        if (_stage==0) {
            if ((_mx > _bx)&&(_mx < _bx+_b0w)) {
                // Within horizontal hit area.
                if ((_my > _b0y)&&(_my < _b0y+_b0h)) {
                    // NEW GAME.
                    _stage=1;
                }

            }
        }

        return true;
    }

    @Override
    public void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        if (_level==0) {
        }
        if (_stage==0) {
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(Color.WHITE);
            paint.setTextSize(24);
            Bitmap _logo = BitmapFactory.decodeResource(getResources(), R.drawable.joeyant);
            Bitmap _newgame = BitmapFactory.decodeResource(getResources(), R.drawable.new_game);
            Bitmap _resumegame = BitmapFactory.decodeResource(getResources(), R.drawable.resume_game);
            Bitmap _controls = BitmapFactory.decodeResource(getResources(), R.drawable.controls);
            Bitmap _credits = BitmapFactory.decodeResource(getResources(), R.drawable.credits);
            _swidth=canvas.getWidth();
            _sheight=canvas.getHeight();
            canvas.drawColor(Color.argb(255, 225, 199, 77));
            canvas.drawBitmap(_logo, (_swidth/2)-(_logo.getWidth()/2), 20, null);
            _bx=(_swidth/2)-(_newgame.getWidth()/2);
            _b0y=150;
            _b1y=210;
            _b2y=270;
            _b3y=330;
            _b0w=_newgame.getWidth();
            _b1w=_resumegame.getWidth();
            _b2w=_controls.getWidth();
            _b3w=_credits.getWidth();
            _b0h=_newgame.getHeight();
            _b1h=_resumegame.getHeight();
            _b2h=_controls.getHeight();
            _b3h=_credits.getHeight();
            canvas.drawBitmap(_newgame, _bx, _b0y, null);
            canvas.drawBitmap(_resumegame, _bx, _b1y, null);
            canvas.drawBitmap(_controls, _bx, _b2y, null);
            canvas.drawBitmap(_credits, _bx, _b3y, null);
        }
        if (_stage==1) {
            // Introduction!
            paint.setColor(Color.BLACK);
            paint.setTextSize(18);
            paint.setStyle(Paint.Style.STROKE);
            Bitmap _intro = BitmapFactory.decodeResource(getResources(), R.drawable.introduction);
            canvas.drawColor(Color.argb(255, 225, 199, 77));
            canvas.drawBitmap(_intro, (_swidth/2)-(_intro.getWidth()/2), (_sheight/2)-(_intro.getHeight()), null);
        }
        if (_stage==2) {
            paint.setColor(Color.WHITE);

            Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
            canvas.drawColor(Color.argb(255, 225, 199, 77));
            canvas.drawBitmap(_scratch, _x, _y, null);
            canvas.drawText(String.valueOf(_x)+","+String.valueOf(_y)+" : "+String.valueOf(_x2)+","+String.valueOf(_y2), 20, 20, paint);
            _width=_scratch.getWidth();
            _height=_scratch.getHeight();
            _angle = Math.atan2(_y2-_y, _x2-_x);
            _xchange=Math.cos(_angle) * _speed;
            _ychange=Math.sin(_angle) * _speed;
            if (_x != _x2) {
                _x+=_xchange;
            }
            if (Math.abs(_x2-_x)<_xchange) {
                _x=_x2;
            }
            if (_y != _y2) {
                _y+=_ychange;
            }
            if (Math.abs(_y2-_y)<_ychange) {
                _y=_y2;
            }
        }
    }

}
class TutorialThread extends Thread {
    private SurfaceHolder _surfaceHolder;
    private Panel _panel;
    private boolean _run = false;

    public TutorialThread(SurfaceHolder surfaceHolder, Panel panel) {
        _surfaceHolder = surfaceHolder;
        _panel = panel;
    }

    public void setRunning(boolean run) {
        _run = run;
    }


    @Override
    public void run() {
        Canvas c;
        while (_run) {
            c = null;
            try {
                c = _surfaceHolder.lockCanvas(null);
                synchronized (_surfaceHolder) {
                    _panel.onDraw(c);
                }
            } finally {
                // do this in a finally so that if an exception is thrown
                // during the above, we don't leave the Surface in an
                // inconsistent state
                if (c != null) {
                    _surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

只是关闭这一个。我重新安装了Eclipse和Android,这种情况不复存在了。