2014-02-19 48 views
0

在运行方法内调用下面的方法时,我总是收到空指针异常。运行方法内部调用方法时出现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在运行方法中调用

+1

timerIsRunning是布尔值?或布尔? – VinayVeluri

+4

对'swCounter'的引用是否为'null'?无论如何,哪一行是'StopwatchTimer.java:57'? – MadProgrammer

+1

“endTime”声明在哪里? –

回答

0

我们需要知道“line 57”是什么,if您可以请提供名为swTimerDisplay的课程。如果没有这个,从这里我只能看到你应该检查的两种可能性,如果你还没有。

  1. 您对StopwatchTimer引用是空的,即没有实例, 当你调用swTimerDisplay()。实际上你会做null.swTimerDisplay()
  2. 在最后一行swTimerDisplay(),swCounter为空。

编辑:我只注意到我会看一点,你的方法是保护的,所以2

+0

第57行是swCounter.setText(display); – user3325917

+0

我在帖子中增加了额外的细节。再次感谢 – user3325917

+0

好的,我使用if语句对swCounter进行了检查,结果确实为null。但我已经使用findViewById在onCreate方法设置swCounter,所以idk为什么它为null – user3325917

相关问题