简体   繁体   中英

Android timer throws Null Pointer Exception

I've made a game in Android and got pretty much everything to work, except the timer. It's throwing a NPE whenever I try to run the game. I've tried a few different timers, looked through loads of pages trying to find an answer but have had no luck so far. I've posted the relevant code and logcat below.

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, the error is at 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 code

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();

    }

The textView is null.

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

will always return null because is called before setContentView() that i think you call in onCreate()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM