有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android计时器引发空指针异常

我用安卓系统做了一个游戏,除了计时器,几乎所有的东西都可以用。每当我尝试运行游戏时,它都会抛出一个NPE。我试过几个不同的计时器,翻阅了大量的页面试图找到答案,但到目前为止运气不佳。我已经在下面发布了相关代码和日志

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 安卓.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
            at 安卓.os.Handler.dispatchMessage(Handler.java:99)
            at 安卓.os.Looper.loop(Looper.java:137)
            at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.安卓.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

游戏活动代码

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) 个答案

  1. # 1 楼答案

    文本视图为空

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

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