有人可以请解释为什么scoreTextSwitcher即使我用if语句检查也会得到空错误吗?改变屏幕方向 - - 错误才刚刚开始活动后,再次抛出改变屏幕的方向 - 然后出现错误...空检查后抛出错误吗?
if(scoreTextSwitcher != null) {
scoreTextSwitcher.setText(String.valueOf(a));
}
我试图寻找下面的链接的答案,但无法弄清楚它是如何涉及到我的代码。如果是这样,有人可以澄清?非常感谢。
- NullReferenceException thrown after null check?
- Null Pointer Exception after checking with 'if' statement
- NullPointerException thrown after field is check if it is null
这里是整个方法(不知道是否在一个线程是要紧)
private void setScoreTextSwitcher(final int a){
Thread setScore = new Thread() {
public void run() {
if(scoreTextSwitcher == null) {
scoreTextSwitcher = (TextSwitcher) findViewById(R.id.score_text_switcher);
}
GameActivity.this.runOnUiThread(new Runnable() {
public void run() {
if(scoreTextSwitcher != null) {
scoreTextSwitcher.setText(String.valueOf(a));
}
}
});
}
};
setScore.start();
}
下面是错误信息:
08-27 11:02:10.226 2780-2780/com.major.color D/AndroidRuntime﹕ Shutting down VM
08-27 11:02:10.226 2780-2780/com.major.color W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x409961f8)
08-27 11:02:10.247 2780-2780/com.major.color E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.TextSwitcher.setText(TextSwitcher.java:78)
at com.major.color.GameActivity$6$1.run(GameActivity.java:600)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
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)
这是我的计划,也越来越空指针错误的另一部分。我相信这是同样的问题,因为它只发生在来回切换屏幕方向时。希望这第二个例子可以帮助缩小一点。 onCreate方法的
private void startCountDownTimer(){
isTimerOn = true;
timerTextSwitcher = (TextSwitcher) findViewById(R.id.timer_text_switcher);
timer = new CountDownTimer(timerCount, 1000) {
public void onTick(long millisUntilFinished) {
isTimerOn = true;
timerCount = millisUntilFinished;
if (millisUntilFinished < 10001) {
TextView TextSwTextView = (TextView) timerTextSwitcher.getChildAt(0);
TextSwTextView.setTextColor(Color.RED);
TextSwTextView.setText(String.valueOf(millisUntilFinished/1000));
}else
if(timerTextSwitcher != null){
timerTextSwitcher.setText(String.valueOf(millisUntilFinished/1000));}else {
timerTextSwitcher = (TextSwitcher) findViewById(R.id.timer_text_switcher);
timerTextSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
public View makeView() {
// Create a new TextView and set properties
TextView textView = new TextView(getApplicationContext());
textView.setLayoutParams(new TextSwitcher.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setTextSize(17);
textView.setTextColor(Color.BLUE);
return textView;
}
});
timerTextSwitcher.setText(String.valueOf(millisUntilFinished/1000));
}
}
public void onFinish() {
timerTextSwitcher.post(new Runnable() {
@Override
public void run() {
createToast("GAME OVER!");
}
});
isTimerOn = false;
saveScore();
DialogFragment endDialog = new EndGameFragment();
Dialog = endDialog;
endgameVisible = true;
endDialog.show(getSupportFragmentManager(), "EndGameDialogFragment");
}
};
timer.start();
}
部分中显示我怎么设置的TextSwitcher的得分和活动的时间。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_game)
//more code...
timerTextSwitcher = (TextSwitcher) findViewById(R.id.timer_text_switcher);
timerTextSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
public View makeView() {
// Create a new TextView and set properties
TextView textView = new TextView(getApplicationContext());
textView.setLayoutParams(new TextSwitcher.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setTextSize(17);
textView.setTextColor(Color.BLUE);
return textView;
}
});
scoreTextSwitcher = (TextSwitcher) findViewById(R.id.score_text_switcher);
scoreTextSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
public View makeView() {
// Create a new TextView and set properties
TextView textView = new TextView(getApplicationContext());
textView.setLayoutParams(new TextSwitcher.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setTextSize(17);
textView.setTextColor(Color.BLUE);
textView.setText("0");
return textView;
}
});
// Declare the animations and initialize them
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
// set the animation type to textSwitcher
timerTextSwitcher.setInAnimation(in);
timerTextSwitcher.setInAnimation(out);
scoreTextSwitcher.setInAnimation(in);
scoreTextSwitcher.setInAnimation(out);
}
你确定'a'不是null吗?并且错误不在'String.valueOf(a)'上? – 2014-08-27 12:06:29
你是否检查了一些日志语句,你的假设是否正确? – stealthjong 2014-08-27 12:07:52
@Dhruti这就是我第一次想到的。我改为100进行测试,但仍然失败。 – cjayem13 2014-08-27 12:39:55