在run方法中调用方法时出现NullPointerException问题

时间:2014-02-19 00:58:00

标签: java android nullpointerexception android-handler

在run方法中调用下面的方法时,我一直得到一个空指针异常。

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);
}

这是log cat输出

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();
            }   
        }
    };



}

以上是我的代码,并在run方法中调用swTimerDisplay

1 个答案:

答案 0 :(得分:0)

我们需要知道“第57行”是什么,如果可以,请提供调用swTimerDisplay的类。没有这个,从这里我只能看到两种可能性,如果你还没有,你应该检查。

  1. 您对StopwatchTimer的引用为null,即尚未实例化, 当你致电swTimerDisplay()时。实际上你正在做null.swTimerDisplay()
  2. swTimerDisplay()的最后一行,swCounter为空。
  3. 编辑:我刚注意到你的方法受到保护所以我会看第2点。