Android计时器抛出Null Pointer Exception

时间:2015-05-11 20:27:27

标签: java android timer nullpointerexception

我已经在Android上制作了一款游戏,除了计时器之外,我还能完成所有工作。每当我尝试运行游戏时,它都会抛出一个NPE。我尝试了几个不同的计时器,看了很多页面试图找到答案但到目前为止没有运气。我在下面发布了相关代码和logcat。

public class GameView extends View {

    private Paint redPaint, blackPaint;
    private Bitmap ball1;
    private String output = "Output will appear here";
    private int ballX, ballY;
    private int radius = 50;
    private int count = 0;

    private int circleX = 350;
    private int circleY = 550;

     TextView tv;

        public GameView(Context context) {
            super(context);
            redPaint = new Paint();
            redPaint.setColor(Color.RED);
            blackPaint = new Paint();
            blackPaint.setColor(Color.BLACK);
            ball1 = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
            tv = (TextView)findViewById(R.id.timer);

            new CountDownTimer(60000, 1000) {
                public void onTick(long millisUntilFinished) {
                    tv.setText("seconds remaining: " + millisUntilFinished / 1000);
                }
                public void onFinish() {
                    tv.setText("done!");
                }
            }.start();

        }

Logcat,错误发生在tv.setText("seconds remaining: " + millisUntilFinished / 1000);

05-11 20:11:59.244      645-645/com.example.jeff.ballgame E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.example.jeff.ballgame.GameView$1.onTick(GameView.java:39)
            at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)
05-11 20:11:59.374      645-651/com.example.jeff.ballgame I/dalvikvm﹕ threadid=3: reacting to signal 3
05-11 20:11:59.394      645-651/com.example.jeff.ballgame I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
05-11 20:11:59.774      645-651/com.example.jeff.ballgame I/dalvikvm﹕ threadid=3: reacting to signal 3
05-11 20:11:59.784      645-651/com.example.jeff.ballgame I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
05-11 20:16:59.628      645-645/com.example.jeff.ballgame I/Process﹕ Sending signal. PID: 645 SIG: 9

GameActivity代码

public class GameActivity extends Activity {

    GameView GV;
    TextView tv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        GV = new GameView(this);
        setContentView(GV);

        tv = (TextView)findViewById(R.id.timer);


        new CountDownTimer(60000, 1000) {
            public void onTick(long millisUntilFinished) {
                tv.setText("seconds remaining: " + millisUntilFinished / 1000);
            }
            public void onFinish() {
                tv.setText("done!");
            }
        }.start();

    }

1 个答案:

答案 0 :(得分:1)

textView为null。

tv = (TextView)findViewById(R.id.timer);

将始终返回null,因为我认为您在setContentView()中调用onCreate()之前调用