Java 空检查后引发空错误?
有人能解释一下为什么scoreTextSwitcher在我用if语句检查时出现空错误吗?只有在启动活动-更改屏幕方向-再次更改屏幕方向-之后才会引发错误Java 空检查后引发空错误?,java,android,if-statement,nullpointerexception,Java,Android,If Statement,Nullpointerexception,有人能解释一下为什么scoreTextSwitcher在我用if语句检查时出现空错误吗?只有在启动活动-更改屏幕方向-再次更改屏幕方向-之后才会引发错误 if(scoreTextSwitcher != null) { scoreTextSwitcher.setText(String.valueOf(a)); } 我试着在下面的链接上寻找答案,但无法找出它与我的代码的关系。如果是的话,有人能澄清一下吗?非常感谢 以下是整个方法(不确定是否在线程中) 以下是错误消息:
if(scoreTextSwitcher != null) {
scoreTextSwitcher.setText(String.valueOf(a));
}
我试着在下面的链接上寻找答案,但无法找出它与我的代码的关系。如果是的话,有人能澄清一下吗?非常感谢
以下是整个方法(不确定是否在线程中)
以下是错误消息:
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)
这里是我的程序的另一部分,它也得到了一个空指针错误。我认为这是同一个问题,因为它只发生在启动和前后改变屏幕方向时。。希望第二个例子能帮助缩小范围
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();
}
试试这个
private void setScoreTextSwitcher(final int a){
scoreTextSwitcher = (TextSwitcher) findViewById(R.id.score_text_switcher);
Thread setScore = new Thread() {
public void run() {
if(scoreTextSwitcher.isEmpty()) {
Log.e("","Empty");
}
GameActivity.this.runOnUiThread(new Runnable() {
public void run() {
if(scoreTextSwitcher != null) {
scoreTextSwitcher.setText(String.valueOf(a));
}
}
});
}
};
setScore.start();
}以下几行导致了不希望出现的行为:
private void startCountDownTimer(){
...
timerTextSwitcher = (TextSwitcher) findViewById(R.id.timer_text_switcher);
以及:
对
findViewById()
的这些调用正在重新初始化切换器,导致它们丢失在onCreate()
中获得的工厂和动画设置,并导致setText()
方法中出现内部NullPointerException
。由于似乎在活动的作用域中声明了切换器,因此您只需删除这些行。您确定a
不为空吗?并且错误不在字符串上。valueOf(a)
?您是否已使用一些日志语句检查您的假设是否正确?TextSwitcher是用于设置文本输入和输出动画的ViewSwitcher。你想要这个还是普通的文本视图?如果您需要交换机,则需要对其进行适当设置。问题不在于scoreTextSwitcher
为空。然后清除对SetCoreTextSwitcher()
中的findViewById()
和startCountDownTimer()的调用。他们正在清除您正在执行的所有设置。我不确定是否在跟踪您,但您认为活动将被重新创建,并且将再次调用onCreate()
。您不需要将任何视图声明为static
;它们将在配置更改时重新实例化和初始化。稍后我会在桌面上发布一个答案。谢谢(顺便说一句,当你在评论中使用@name时,去掉所有的空格。)我将经常调用这个方法;继续使用findViewById()方法可以吗?我觉得这样做很费时。我希望避免每次需要更改分数时调用它,只有当为空时,我仍然会得到相同的错误。我在另一个部分得到了一个类似的。我编辑了我的帖子。另外,isEmpty()是TextView的方法,而不是文本切换器。
private void setScoreTextSwitcher(final int a){
scoreTextSwitcher = (TextSwitcher) findViewById(R.id.score_text_switcher);
Thread setScore = new Thread() {
public void run() {
if(scoreTextSwitcher.isEmpty()) {
Log.e("","Empty");
}
GameActivity.this.runOnUiThread(new Runnable() {
public void run() {
if(scoreTextSwitcher != null) {
scoreTextSwitcher.setText(String.valueOf(a));
}
}
});
}
};
setScore.start();
private void startCountDownTimer(){
...
timerTextSwitcher = (TextSwitcher) findViewById(R.id.timer_text_switcher);
private void setScoreTextSwitcher(final int a){
...
scoreTextSwitcher = (TextSwitcher) findViewById(R.id.score_text_switcher);