我使用Runnable
作为Timer
,或者运行一个区块。
我从here获得了此代码,并将其更改为:
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);
}
}
...并且使用的onCreate
方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TikingTimer tt = new TikingTimer(1000);
tt.Start();
}
但是当我运行项目时,它会强行关闭。
在Runnable
方法中使用onCreate
是否错误?
感谢。
LogCat堆栈跟踪是:
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
答案 0 :(得分:1)
在您的MainActivity(未发布)中, 14 行,您正在查找View
方法:
findViewById(Activity.java:1839)
...找不到。
由于您以某种方式操纵此视图,因此会抛出您发布的NullPointerException
。
答案 1 :(得分:1)
您正在尝试访问未在主要活动的xml布局中定义的视图。
检查MainActivity上的第14行
此问题不是由于Runnable。