【发布时间】:2014-03-19 02:15:34
【问题描述】:
在运行方法中调用以下方法时,我不断收到空指针异常。
protected void swTimerDisplay()
{
String display;
long now;
long diff;
long secs;
long mins;
long hours;
if(timerIsRunning)
{
now = System.currentTimeMillis();
}else{
now = endTime;
}
diff = now - startTime;
if(diff < 0)
{
diff = 0;
}
/**
* Calculate the
* secs,mins and hours
*/
secs = diff / 1000;
mins = secs / 60;
hours = mins / 60;
secs = secs % 60;
mins = mins % 60;
display = String.format("%d", hours) + ":" +
String.format("%02d", mins) + ":" +
String.format("%02d", secs);
swCounter.setText(display);
}
这里是日志猫输出
02-18 19:47:27.143: E/AndroidRuntime(1695): FATAL EXCEPTION: main
02-18 19:47:27.143: E/AndroidRuntime(1695): Process: com.webdeveloper93.profitnessstopwatch, PID: 1695
02-18 19:47:27.143: E/AndroidRuntime(1695): java.lang.NullPointerException
02-18 19:47:27.143: E/AndroidRuntime(1695): at com.webdeveloper93.profitnessstopwatch.StopwatchTimer.swTimerDisplay(StopwatchTimer.java:57)
02-18 19:47:27.143: E/AndroidRuntime(1695): at com.webdeveloper93.profitnessstopwatch.timerUpdate.run(timerUpdate.java:22)
02-18 19:47:27.143: E/AndroidRuntime(1695): at android.os.Handler.handleCallback(Handler.java:733)
02-18 19:47:27.143: E/AndroidRuntime(1695): at android.os.Handler.dispatchMessage(Handler.java:95)
02-18 19:47:27.143: E/AndroidRuntime(1695): at android.os.Looper.loop(Looper.java:137)
02-18 19:47:27.143: E/AndroidRuntime(1695): at android.app.ActivityThread.main(ActivityThread.java:4998)
02-18 19:47:27.143: E/AndroidRuntime(1695): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 19:47:27.143: E/AndroidRuntime(1695): at java.lang.reflect.Method.invoke(Method.java:515)
02-18 19:47:27.143: E/AndroidRuntime(1695): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
感谢您提供的任何帮助。
编辑:
第 57 行是 swCounter.setText(display);
下面是 swTimerDisplay();被称为
public class timerUpdate extends StopwatchTimer implements Runnable {
@Override
public void run() {
swTimerDisplay();
if(handle != null)
{
handle.postDelayed(this, UPDATE);
}
}
}
希望有人能帮我解决这个问题,因为它让我发疯:) 再次感谢
编辑: 这里有更多的代码要求
public class Stopwatch extends Activity implements OnClickListener {
private StopwatchTimer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stopwatch);
//Create new instance of StopwatchTimer
timer = new StopwatchTimer();
timer.swCounter = (TextView) findViewById(R.id.swcounter);
timer.start = (Button) findViewById(R.id.start);
timer.stop = (Button) findViewById(R.id.stop);
timer.start.setOnClickListener(this);
timer.stop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v==timer.start)
{
timer.timerStart();
}else if(v==timer.stop)
{
timer.timerStop();
}
}
public final Runnable timerUpdate = new Runnable(){
public void run(){
try {
timer.swTimerDisplay();
if(timer.handle != null)
{
timer.handle.postDelayed(this, StopwatchTimer.UPDATE);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
};
}
以上是我的代码,在run方法中调用了swTimerDisplay
【问题讨论】:
-
timerIsRunning 是布尔值吗?还是布尔值?
-
对
swCounter的引用可以是null吗?StopwatchTimer.java:57到底是哪一行? -
endTime声明在哪里? -
第 57 行是哪一行?
-
如果有的话,使用调试器设置断点和检查变量可能比玩“猜测空变量”游戏更成功。调试器很棒,因为您没有猜测。你可以看到什么是空的,什么不是。
标签: java android nullpointerexception android-handler