0

I'm using a Runnable as a Timer that alternatively runs a block.

I got this code from here and changed it as such:

public class TikingTimer {

    private long countDownInterval;
    private boolean status;
    public TikingTimer(long pCountDownInterval) {
        this.countDownInterval = pCountDownInterval;
        status = false;
        Initialize();
    }

    public void Stop() {
        status = false;
    }

    public void Start() {
        status = true;
    }
    public void Initialize() 
    {
        final Handler handler = new Handler();
        final Runnable counter = new Runnable(){

            public void run(){
                if(status) {


                    //TODO my code


                } else {
                    handler.postDelayed(this, countDownInterval);
                }
            }
        };

        handler.postDelayed(counter, countDownInterval);
    }
}

... and at onCreate method I used :

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        TikingTimer tt = new TikingTimer(1000);
        tt.Start();
    }

But when I run the project, it force-closes.

Is it wrong to use Runnable at onCreate method?

Thanks.

The LogCat stack trace is:

08-11 13:04:55.384: E/AndroidRuntime(3125): FATAL EXCEPTION: main
08-11 13:04:55.384: E/AndroidRuntime(3125): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{kawthar.taghvimdigital/kawthar.taghvimdigital.MainActivity}: java.lang.NullPointerException
08-11 13:04:55.384: E/AndroidRuntime(3125):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
08-11 13:04:55.384: E/AndroidRuntime(3125):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-11 13:04:55.384: E/AndroidRuntime(3125):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-11 13:04:55.384: E/AndroidRuntime(3125):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-11 13:04:55.384: E/AndroidRuntime(3125):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 13:04:55.384: E/AndroidRuntime(3125):     at android.os.Looper.loop(Looper.java:137)
08-11 13:04:55.384: E/AndroidRuntime(3125):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-11 13:04:55.384: E/AndroidRuntime(3125):     at java.lang.reflect.Method.invokeNative(Native Method)
08-11 13:04:55.384: E/AndroidRuntime(3125):     at java.lang.reflect.Method.invoke(Method.java:511)
08-11 13:04:55.384: E/AndroidRuntime(3125):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-11 13:04:55.384: E/AndroidRuntime(3125):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-11 13:04:55.384: E/AndroidRuntime(3125):     at dalvik.system.NativeStart.main(Native Method)
08-11 13:04:55.384: E/AndroidRuntime(3125): Caused by: java.lang.NullPointerException
08-11 13:04:55.384: E/AndroidRuntime(3125):     at android.app.Activity.findViewById(Activity.java:1839)
08-11 13:04:55.384: E/AndroidRuntime(3125):     at kawthar.taghvimdigital.MainActivity.<init>(MainActivity.java:14)
08-11 13:04:55.384: E/AndroidRuntime(3125):     at java.lang.Class.newInstanceImpl(Native Method)
08-11 13:04:55.384: E/AndroidRuntime(3125):     at java.lang.Class.newInstance(Class.java:1319)
08-11 13:04:55.384: E/AndroidRuntime(3125):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
08-11 13:04:55.384: E/AndroidRuntime(3125):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
08-11 13:04:55.384: E/AndroidRuntime(3125):     ... 11 more
4

2 回答 2

1

您正在尝试访问未在主要活动的 xml 布局中定义的视图。

检查 MainActivity 上的第 14 行

此问题不是由于 Runnable 造成的。

于 2013-08-11T09:16:00.503 回答
1

在您的 MainActivity (未发布)中,在第14行,您正在查找一个Viewthrough 方法:

findViewById(Activity.java:1839)

...没有找到。

由于您以某种方式操纵此视图,因此NullPointerException您发布的内容将被抛出。

于 2013-08-11T09:11:57.280 回答