[英]Android timer throws Null Pointer Exception
I've made a game in Android and got pretty much everything to work, except the timer. 我用Android制作了游戏,除了计时器外,几乎所有其他功能都可以使用。 It's throwing a NPE whenever I try to run the game.
每当我尝试运行游戏时,都会抛出NPE。 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.
我已经在下面发布了相关的代码和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, the error is at tv.setText("seconds remaining: " + millisUntilFinished / 1000);
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 code 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();
}
The textView is null. textView为null。
tv = (TextView)findViewById(R.id.timer);
will always return null because is called before setContentView()
that i think you call in onCreate()
将始终返回null,因为在我认为您在
onCreate()
调用的setContentView()
之前被调用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.