2013-07-29 69 views
0

我正在嘗試在應用程序中每隔一分鐘執行一次任務,我使用相同的以下內容。該代碼位於onCreate方法中:在計時器任務中獲取NPE

 mTimer.scheduleAtFixedRate(new TimerTask() { 
     @Override 
     public void run() { 
      PageTask();// The task I want to exceute every 1 minute. 
     } 
    }, 0, REFRESH_TIME); 

我聲明在類級別以下內容:

 Timer mTimer = null; 
     long REFRESH_TIME = 10000; 

然而,當我運行程序我在mTimer.scheduleAtFixedRate(新的TimerTask獲得NPE( )

我是不是走錯了地方

登錄貓:?

  07-29 15:57:28.906: E/AndroidRuntime(15415): at          com.Geet.CustomizedListView.onCreate(CustomizedListView.java:93) 
     07-29 16:01:16.179: E/AndroidRuntime(15748): FATAL EXCEPTION: main 
     07-29 16:01:16.179: E/AndroidRuntime(15748): java.lang.RuntimeException:    Unable to start activity ComponentInfo{com.Geet/com.Geet.CustomizedListView}:    java.lang.NullPointerException 
     07-29 16:01:16.179: E/AndroidRuntime(15748): at    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097) 
     07-29 16:01:16.179: E/AndroidRuntime(15748): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122) 
     07-29 16:01:16.179: E/AndroidRuntime(15748): at android.app.ActivityThread.access$600(ActivityThread.java:140) 
     07-29 16:01:16.179: E/AndroidRuntime(15748): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228) 
     07-29 16:01:16.179: E/AndroidRuntime(15748): at android.os.Handler.dispatchMessage(Handler.java:99) 
     07-29 16:01:16.179: E/AndroidRuntime(15748): at android.os.Looper.loop(Looper.java:137) 
     07-29 16:01:16.179: E/AndroidRuntime(15748): at android.app.ActivityThread.main(ActivityThread.java:4895) 
     07-29 16:01:16.179: E/AndroidRuntime(15748): at java.lang.reflect.Method.invokeNative(Native Method) 
     07-29 16:01:16.179: E/AndroidRuntime(15748): at java.lang.reflect.Method.invoke(Method.java:511) 
     07-29 16:01:16.179: E/AndroidRuntime(15748): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994) 
     07-29 16:01:16.179: E/AndroidRuntime(15748): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761) 
     07-29 16:01:16.179: E/AndroidRuntime(15748): at dalvik.system.NativeStart.main(Native Method) 
     07-29 16:01:16.179: E/AndroidRuntime(15748): Caused by: java.lang.NullPointerException 
     07-29 16:01:16.179: E/AndroidRuntime(15748): at com.Geet.CustomizedListView.onCreate(CustomizedListView.java:94) 
     07-29 16:01:16.179: E/AndroidRuntime(15748): at android.app.Activity.performCreate(Activity.java:5163) 
     07-29 16:01:16.179: E/AndroidRuntime(15748): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 
     07-29 16:01:16.179: E/AndroidRuntime(15748): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061) 
     07-29 16:01:16.179: E/AndroidRuntime(15748): ... 11 more 
+1

你初始化定時器'mTimer =新的Timer() '? – Raghunandan

+0

男人我很笨! – user2613996

回答

2

我想你還沒有初始化計時器

 mTimer = new Timer(); 

您可以檢查此鏈接爲一個側面說明

Android Thread for a timer

+0

似乎,我需要一杯咖啡,謝謝你的伴侶! – user2613996

+0

@ Stacks28我刪除了評論。 – Raghunandan

+0

@Raghunandan:)np我只是說你值得+1 – KOTIOS