如果在处理时单击,Android 模拟器会崩溃

发布于 2024-12-26 05:02:03 字数 4355 浏览 3 评论 0原文

我一直在开发一个 Android 应用程序并在模拟器上测试它。此应用程序的某些部分需要一些处理时间,例如登录,可能需要长达 10 秒的时间。如果应用程序正在处理登录,并且我单击了模拟器屏幕上的任意位置,则会导致应用程序崩溃 ->将弹出强制关闭/等待窗口,即使我选择等待,此登录过程也永远不会完成。

请问还有人遇到过这种情况吗?我该怎么做才能避免用户在处理过程中点击并导致程序崩溃?或者在真正的手机上这不是问题?

谢谢!

编辑:这确实是一个 UI 线程问题,我正在使用 Mono for Android 来编写这个应用程序,我使用引入的方法来解决问题 这里!

编辑: 这是 LOGCAT 消息

1-11 00:38:49.165 W/WindowManager(   59): Key dispatching timed out sending to BadumnaAndroidApi1.BadumnaAndroidApi1/badumnaandroidapi1.Activity1
01-11 00:38:49.165 W/WindowManager(   59): Previous dispatch state: {{KeyEvent{action=1 code=66 repeat=0 meta=0 scancode=28 mFlags=8} to Window{44ed87d0 BadumnaAndroidApi1.BadumnaAndroidApi1/badumnaandroidapi1.Activity1 paused=false} @ 1326241690617 lw=Window{44ed87d0 BadumnaAndroidApi1.BadumnaAndroidApi1/badumnaandroidapi1.Activity1 paused=false} lb=android.os.BinderProxy@4508a900 fin=false gfw=true ed=true tts=0 wf=false fp=false mcf=Window{44ed87d0 BadumnaAndroidApi1.BadumnaAndroidApi1/badumnaandroidapi1.Activity1 paused=false}}}
01-11 00:38:49.175 W/WindowManager(   59): Current dispatch state: {{null to Window{44ed87d0 BadumnaAndroidApi1.BadumnaAndroidApi1/badumnaandroidapi1.Activity1 paused=false} @ 1326242329174 lw=Window{44ed87d0 BadumnaAndroidApi1.BadumnaAndroidApi1/badumnaandroidapi1.Activity1 paused=false} lb=android.os.BinderProxy@4508a900 fin=false gfw=true ed=true tts=0 wf=false fp=false mcf=Window{44ed87d0 BadumnaAndroidApi1.BadumnaAndroidApi1/badumnaandroidapi1.Activity1 paused=false}}}
01-11 00:38:49.276 I/Process (   59): Sending signal. PID: 316 SIG: 3
01-11 00:38:49.276 I/dalvikvm(  316): threadid=3: reacting to signal 3
01-11 00:38:49.485 I/dalvikvm(  316): Wrote stack traces to '/data/anr/traces.txt'
01-11 00:38:49.485 I/Process (   59): Sending signal. PID: 59 SIG: 3
01-11 00:38:49.485 I/dalvikvm(   59): threadid=3: reacting to signal 3
01-11 00:38:49.595 I/dalvikvm(   59): Wrote stack traces to '/data/anr/traces.txt'
01-11 00:38:49.616 I/Process (   59): Sending signal. PID: 114 SIG: 3
01-11 00:38:49.616 I/dalvikvm(  114): threadid=3: reacting to signal 3
01-11 00:38:49.625 I/dalvikvm(  114): Wrote stack traces to '/data/anr/traces.txt'
01-11 00:38:49.636 I/Process (   59): Sending signal. PID: 107 SIG: 3
01-11 00:38:49.645 I/dalvikvm(  107): threadid=3: reacting to signal 3
01-11 00:38:49.655 I/dalvikvm(  107): Wrote stack traces to '/data/anr/traces.txt'
01-11 00:38:49.673 I/Process (   59): Sending signal. PID: 254 SIG: 3
01-11 00:38:49.865 I/dalvikvm(  254): threadid=3: reacting to signal 3
01-11 00:38:49.875 I/Process (   59): Sending signal. PID: 246 SIG: 3
01-11 00:38:49.906 I/dalvikvm(  246): threadid=3: reacting to signal 3
01-11 00:38:50.085 I/Process (   59): Sending signal. PID: 177 SIG: 3
01-11 00:38:50.115 I/dalvikvm(  177): threadid=3: reacting to signal 3
01-11 00:38:50.285 I/Process (   59): Sending signal. PID: 221 SIG: 3
01-11 00:38:50.345 I/dalvikvm(  221): threadid=3: reacting to signal 3
01-11 00:38:50.495 I/Process (   59): Sending signal. PID: 169 SIG: 3
01-11 00:38:50.575 I/dalvikvm(  169): threadid=3: reacting to signal 3
01-11 00:38:50.695 I/Process (   59): Sending signal. PID: 185 SIG: 3
01-11 00:38:50.735 I/dalvikvm(  185): threadid=3: reacting to signal 3
01-11 00:38:50.905 I/Process (   59): Sending signal. PID: 111 SIG: 3
01-11 00:38:50.905 I/dalvikvm(  111): threadid=3: reacting to signal 3
01-11 00:38:51.065 I/dalvikvm(  111): Wrote stack traces to '/data/anr/traces.txt'
01-11 00:38:51.065 I/Process (   59): Sending signal. PID: 205 SIG: 3
01-11 00:38:51.185 I/dalvikvm(  205): threadid=3: reacting to signal 3
01-11 00:38:51.275 I/Process (   59): Sending signal. PID: 194 SIG: 3
01-11 00:38:51.405 I/dalvikvm(  194): threadid=3: reacting to signal 3
01-11 00:38:51.485 I/Process (   59): Sending signal. PID: 155 SIG: 3
01-11 00:38:51.605 I/dalvikvm(  155): threadid=3: reacting to signal 3
01-11 00:38:51.685 I/Process (   59): Sending signal. PID: 149 SIG: 3
01-11 00:38:51.875 I/dalvikvm(  149): threadid=3: reacting to signal 3
01-11 00:38:51.885 I/Process (   59): Sending signal. PID: 115 SIG: 3
01-11 00:38:51.916 I/dalvikvm(  115): threadid=3: reacting to signal 3

I've been working on an android app and test it on the emulator. Parts of this app require some processing time, such as login, could take up to 10 seconds. If the app is processing the login, and I clicked anywhere on the emulator screen, it will cause the app to crash -> a force close/wait window will pop up and even I choose to wait, this login process will never complete.

I wonder did anybody else encounter this situation? What could I do to avoid user clicking during processing and crash the program? Or on a real phone it is not a problem?

Thanks!

EDIT: This is indeed a UI thread problem and I'm using Mono for Android to write this app, I sort my issues out using methods introduced Here!

EDIT:
this is the LOGCAT messages

1-11 00:38:49.165 W/WindowManager(   59): Key dispatching timed out sending to BadumnaAndroidApi1.BadumnaAndroidApi1/badumnaandroidapi1.Activity1
01-11 00:38:49.165 W/WindowManager(   59): Previous dispatch state: {{KeyEvent{action=1 code=66 repeat=0 meta=0 scancode=28 mFlags=8} to Window{44ed87d0 BadumnaAndroidApi1.BadumnaAndroidApi1/badumnaandroidapi1.Activity1 paused=false} @ 1326241690617 lw=Window{44ed87d0 BadumnaAndroidApi1.BadumnaAndroidApi1/badumnaandroidapi1.Activity1 paused=false} lb=android.os.BinderProxy@4508a900 fin=false gfw=true ed=true tts=0 wf=false fp=false mcf=Window{44ed87d0 BadumnaAndroidApi1.BadumnaAndroidApi1/badumnaandroidapi1.Activity1 paused=false}}}
01-11 00:38:49.175 W/WindowManager(   59): Current dispatch state: {{null to Window{44ed87d0 BadumnaAndroidApi1.BadumnaAndroidApi1/badumnaandroidapi1.Activity1 paused=false} @ 1326242329174 lw=Window{44ed87d0 BadumnaAndroidApi1.BadumnaAndroidApi1/badumnaandroidapi1.Activity1 paused=false} lb=android.os.BinderProxy@4508a900 fin=false gfw=true ed=true tts=0 wf=false fp=false mcf=Window{44ed87d0 BadumnaAndroidApi1.BadumnaAndroidApi1/badumnaandroidapi1.Activity1 paused=false}}}
01-11 00:38:49.276 I/Process (   59): Sending signal. PID: 316 SIG: 3
01-11 00:38:49.276 I/dalvikvm(  316): threadid=3: reacting to signal 3
01-11 00:38:49.485 I/dalvikvm(  316): Wrote stack traces to '/data/anr/traces.txt'
01-11 00:38:49.485 I/Process (   59): Sending signal. PID: 59 SIG: 3
01-11 00:38:49.485 I/dalvikvm(   59): threadid=3: reacting to signal 3
01-11 00:38:49.595 I/dalvikvm(   59): Wrote stack traces to '/data/anr/traces.txt'
01-11 00:38:49.616 I/Process (   59): Sending signal. PID: 114 SIG: 3
01-11 00:38:49.616 I/dalvikvm(  114): threadid=3: reacting to signal 3
01-11 00:38:49.625 I/dalvikvm(  114): Wrote stack traces to '/data/anr/traces.txt'
01-11 00:38:49.636 I/Process (   59): Sending signal. PID: 107 SIG: 3
01-11 00:38:49.645 I/dalvikvm(  107): threadid=3: reacting to signal 3
01-11 00:38:49.655 I/dalvikvm(  107): Wrote stack traces to '/data/anr/traces.txt'
01-11 00:38:49.673 I/Process (   59): Sending signal. PID: 254 SIG: 3
01-11 00:38:49.865 I/dalvikvm(  254): threadid=3: reacting to signal 3
01-11 00:38:49.875 I/Process (   59): Sending signal. PID: 246 SIG: 3
01-11 00:38:49.906 I/dalvikvm(  246): threadid=3: reacting to signal 3
01-11 00:38:50.085 I/Process (   59): Sending signal. PID: 177 SIG: 3
01-11 00:38:50.115 I/dalvikvm(  177): threadid=3: reacting to signal 3
01-11 00:38:50.285 I/Process (   59): Sending signal. PID: 221 SIG: 3
01-11 00:38:50.345 I/dalvikvm(  221): threadid=3: reacting to signal 3
01-11 00:38:50.495 I/Process (   59): Sending signal. PID: 169 SIG: 3
01-11 00:38:50.575 I/dalvikvm(  169): threadid=3: reacting to signal 3
01-11 00:38:50.695 I/Process (   59): Sending signal. PID: 185 SIG: 3
01-11 00:38:50.735 I/dalvikvm(  185): threadid=3: reacting to signal 3
01-11 00:38:50.905 I/Process (   59): Sending signal. PID: 111 SIG: 3
01-11 00:38:50.905 I/dalvikvm(  111): threadid=3: reacting to signal 3
01-11 00:38:51.065 I/dalvikvm(  111): Wrote stack traces to '/data/anr/traces.txt'
01-11 00:38:51.065 I/Process (   59): Sending signal. PID: 205 SIG: 3
01-11 00:38:51.185 I/dalvikvm(  205): threadid=3: reacting to signal 3
01-11 00:38:51.275 I/Process (   59): Sending signal. PID: 194 SIG: 3
01-11 00:38:51.405 I/dalvikvm(  194): threadid=3: reacting to signal 3
01-11 00:38:51.485 I/Process (   59): Sending signal. PID: 155 SIG: 3
01-11 00:38:51.605 I/dalvikvm(  155): threadid=3: reacting to signal 3
01-11 00:38:51.685 I/Process (   59): Sending signal. PID: 149 SIG: 3
01-11 00:38:51.875 I/dalvikvm(  149): threadid=3: reacting to signal 3
01-11 00:38:51.885 I/Process (   59): Sending signal. PID: 115 SIG: 3
01-11 00:38:51.916 I/dalvikvm(  115): threadid=3: reacting to signal 3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半枫 2025-01-02 05:02:03

好吧,您可能可能会因为这种繁重的处理而导致 UI 线程挂起 - 但我可能会在看不到任何代码 - 提示 - 的情况下走开。您可能是将繁重的工作放入与您当前正在其中运行的GUI线程分开的后台线程中。

幸运的是,Android具有AsyncTask 类可以在这里为您提供帮助。假设(因为问题中没有足够的细节)您通过向服务器发出 Web 请求来登录您的应用程序。当用户按下登录按钮时,您希望在后台线程中完成所有跑腿工作时显示某种处理消息。

请查看本文,特别是 AsyncTask 示例 http://www.vogella.de/ articles/AndroidPerformance/article.html

您可以看到 doInBackground() 是完成所有繁重工作的地方,postExecute() 再次在 UI 线程中运行,这是您想要更新基于 UI 的地方在刚刚发生的事情的结果。例如,用户成功登录或登录时发生错误。

我可以继续很长一段时间,但是这是有详细记录的。 Google 自己的另一篇好文章可以在这里找到 http://android-developers .blogspot.com/2009/05/painless-threading.html

Ok the chances are you might be causing the UI thread to hang due to this heavy processing -but I could be way off without seeing any code -hint-. What you might is put the heavy stuff into a background thread seperate to the GUI thread, which you are currently running in.

Luckily Android has the AsyncTask class to help you here. Lets say (as there is not enough detail in the question) you are logging in to your app by making a web request to a server. When the user presses login you want to show some sort of processing message while doing all the legwork in your background thread.

Take a look at this article in particular the AsyncTask example http://www.vogella.de/articles/AndroidPerformance/article.html

You can see that doInBackground() is where all the heavy lifting is done and that postExecute() runs in the UI thread once again, which is where you will want to update the UI based on the result of what just happened. For example the user logged in sucessfully or an error occured while logging in.

I could go on for quite some time however this is well documented. Another good post by Google themselves can be found here http://android-developers.blogspot.com/2009/05/painless-threading.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文