gpt4 book ai didi

java - 在计时器任务中获取 NPE

转载 作者:行者123 更新时间:2023-12-02 06:51:59 25 4
gpt4 key购买 nike

我试图在我的应用程序中每隔一分钟执行一项任务,我使用以下内容来实现相同的目的。代码位于onCreate方法内部:

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

我在类(class)级别声明以下内容:

      Timer mTimer = null;
long REFRESH_TIME = 10000;

但是,当我运行该程序时,我在 mTimer.scheduleAtFixedRate(new 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

最佳答案

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

     mTimer = new Timer();

您可以检查此链接作为附注

Android Thread for a timer

关于java - 在计时器任务中获取 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17921849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com