gpt4 book ai didi

android - 在线程android中使用处理程序

转载 作者:行者123 更新时间:2023-11-30 01:58:00 27 4
gpt4 key购买 nike

我正在尝试在线程内运行处理程序,但出现错误“无法在未调用 Looper.prepare() 的线程内创建处理程序”我花了数小时寻找解决方案,但找不到所以我决定发布这个。我试过调用“Looper.prepare();”,它修复了强制关闭问题,但阻止了处理程序中的代码工作。

    public void ten(View view) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
private Vibrator mVibrator;
private Runnable runnable;
private Handler handler;

public void run() {
mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
mVibrator.vibrate(1000 * 10);// 10 sec sprint

// HANDLER
handler = new Handler();
runnable = new Runnable() {
public void run() {
// calculate result1
TextView theFact = (TextView) findViewById(R.id.txtCurrentSpeed);
String shareFact = theFact.getText().toString();
TextView theFact1 = (TextView) findViewById(R.id.result1);
theFact1.setText(String.valueOf(shareFact));
// calculate result1
}
};
handler.postDelayed(runnable, 3000);
// HANDLER END //
}
}, 5000, 60 * 1000 * 3);// 3 minute break (+5s first run delay)
}
}



08-10 21:37:59.187: E/AndroidRuntime(26129): FATAL EXCEPTION: Timer-0
08-10 21:37:59.187: E/AndroidRuntime(26129): Process: com.example.speedometer, PID: 26129
08-10 21:37:59.187: E/AndroidRuntime(26129): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-10 21:37:59.187: E/AndroidRuntime(26129): at android.os.Handler.<init>(Handler.java:200)
08-10 21:37:59.187: E/AndroidRuntime(26129): at android.os.Handler.<init>(Handler.java:114)
08-10 21:37:59.187: E/AndroidRuntime(26129): at com.example.speedometer.MainActivity$2.run(MainActivity.java:141)
08-10 21:37:59.187: E/AndroidRuntime(26129): at java.util.Timer$TimerImpl.run(Timer.java:284)

我用 Thread 替换了 Handler,线程内的代码有效,但是它强制关闭并给出以下异常:

08-10 21:57:35.477: E/AndroidRuntime(31261): FATAL EXCEPTION: Thread-4399
08-10 21:57:35.477: E/AndroidRuntime(31261): Process: com.example.speedometer, PID: 31261
08-10 21:57:35.477: E/AndroidRuntime(31261): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-10 21:57:35.477: E/AndroidRuntime(31261): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7147)
08-10 21:57:35.477: E/AndroidRuntime(31261): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:998)
08-10 21:57:35.477: E/AndroidRuntime(31261): at android.view.View.requestLayout(View.java:18491)
08-10 21:57:35.477: E/AndroidRuntime(31261): at android.view.View.requestLayout(View.java:18491)
08-10 21:57:35.477: E/AndroidRuntime(31261): at android.view.View.requestLayout(View.java:18491)
08-10 21:57:35.477: E/AndroidRuntime(31261): at android.view.View.requestLayout(View.java:18491)
08-10 21:57:35.477: E/AndroidRuntime(31261): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:361)
08-10 21:57:35.477: E/AndroidRuntime(31261): at android.view.View.requestLayout(View.java:18491)
08-10 21:57:35.477: E/AndroidRuntime(31261): at android.widget.TextView.checkForRelayout(TextView.java:8060)
08-10 21:57:35.477: E/AndroidRuntime(31261): at android.widget.TextView.setText(TextView.java:4836)
08-10 21:57:35.477: E/AndroidRuntime(31261): at android.widget.TextView.setText(TextView.java:4660)
08-10 21:57:35.477: E/AndroidRuntime(31261): at android.widget.TextView.setText(TextView.java:4635)
08-10 21:57:35.477: E/AndroidRuntime(31261): at com.example.speedometer.MainActivity$2$1.run(MainActivity.java:154)
08-10 21:57:35.477: E/AndroidRuntime(31261): at java.lang.Thread.run(Thread.java:818)

最佳答案

android的Main/UI线程默认有一个looper。因此,如果您在方法在 UI 线程上运行的 Activity/Fragment/View 中的任何位置创建 Handler 的实例,它将使用默认的 Looper 并且永远不会抛出异常。

因此,最好在主/UI 线程上创建 Handler 来更新 UI。

话虽如此,解决您的解决方案的最简单方法是像这样将默认 Looper 作为参数传递

 handler = new Handler(Looper.getMainLooper());

这将解决您尝试更新 UI 时遇到的问题。

关于android - 在线程android中使用处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31924332/

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