gpt4 book ai didi

android - Android AsyncTask崩溃

转载 作者:行者123 更新时间:2023-12-03 16:19:21 26 4
gpt4 key购买 nike

我正在扩展AsyncTask,并在我的doInBackground()中使用findViewById获得按钮 View ,并且当我调用button.performClick()时,我的应用程序崩溃了。

知道为什么吗?

这是logcat:

E/AndroidRuntime(604): FATAL EXCEPTION: AsyncTask #2
E/AndroidRuntime(604): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(604): at android.os.AsyncTask$3.done(AsyncTask.java:278)
E/AndroidRuntime(604): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
E/AndroidRuntime(604): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
E/AndroidRuntime(604): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
E/AndroidRuntime(604): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
E/AndroidRuntime(604): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
E/AndroidRuntime(604): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
E/AndroidRuntime(604): at java.lang.Thread.run(Thread.java:856)
E/AndroidRuntime(604): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
E/AndroidRuntime(604): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4039)
E/AndroidRuntime(604): at android.view.ViewRootImpl.playSoundEffect(ViewRootImpl.java:3610)
E/AndroidRuntime(604): at android.view.View.playSoundEffect(View.java:13103)
E/AndroidRuntime(604): at android.view.View.performClick(View.java:3510)

最佳答案

I'm extending AsyncTask and in my doInBackground() I get a button view using findViewById and when I call button.performClick() my app crashes.



在AsyncTasks中,您只能使用 onProgressUpdate()onPostExecute()或有权访问UI线程的其他方法中的UI元素(例如Button)。只需将调用 button的代码移到可接受的方法中即可。

关于android - Android AsyncTask崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13664986/

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