在计时器任务中获得NPE

时间:2013-07-29 10:36:54

标签: java android multithreading

我正在尝试在我的应用程序中每隔一分钟执行一次任务,我使用以下内容进行相同操作。代码位于onCreate方法中:

      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

我在某个地方出错了吗?

Log Cat:

         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 个答案:

答案 0 :(得分:2)

我猜你还没有初始化计时器

     mTimer = new Timer();

您可以将此链接视为旁注

Android Thread for a timer