gpt4 book ai didi

java - 无法使用 Runnable

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

我使用一个Runnable作为Timer来运行一个 block 。

我从 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();
}

但是当我运行该项目时,它强制关闭。

onCreate方法中使用Runnable是否错误?

谢谢。

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

最佳答案

在您的 MainActivity(未发布)中,在第 14 行,您正在通过方法查找 View:

findViewById(Activity.java:1839)

...未找到。

由于您以某种方式操纵此 View ,因此会抛出您发布的 NullPointerException

关于java - 无法使用 Runnable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18170330/

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