在运行方法内调用下面的方法时,我总是收到空指针异常。运行方法内部调用方法时出现NullPointerException
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(显示器);
下面是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();
}
}
};
}
以上是我的代码,swTimerDisplay在运行方法中调用
timerIsRunning是布尔值?或布尔? – VinayVeluri
对'swCounter'的引用是否为'null'?无论如何,哪一行是'StopwatchTimer.java:57'? – MadProgrammer
“endTime”声明在哪里? –