多核处理器上的 Android 错误
我正在开发一个多线程应用程序,它使用线程和异步任务在多个线程上执行代码。
我有一个 AsyncTask,它向数据库添加一行,然后发送 API 请求。收到 API 请求回复后,它会更新数据库中的行。数据库适配器对象从 UI 线程传递到 AsyncTask。
不幸的是,AsyncTask 不适用于多核设备(例如 Asus Transformer)。当我第一次尝试将该行添加到数据库时,发生空指针异常。
我不知道为什么会发生这种情况,但这可能与 “内存可观察性”下的文档。
I am developing a multi threaded application, that uses both threads and Async tasks to execute code on multiple threads.
I have an AsyncTask that adds a row to a database, and then sends off an API request. Once the API request reply is received, it updates the row in the database. The database adapter object is passed to the AsyncTask from the UI thread.
Unfortunately, the AsyncTask doesn't work on multi core devices (such as the Asus Transformer). A null pointer exception occurs when I try to add the row to the database for the first time.
I don't know why this happens, but it may be something to do with what is says in the docs under 'Memory Observability'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
谢谢大家的评论。
事实证明我确实有竞争条件。我没有意识到onActivityResult是在onResume之前调用的,所以事实证明,在较慢的手机上,可运行任务将在调用onResume之后执行(因此数据库已打开),但在较快的手机上,可运行任务将被执行将首先进行数据库操作并遇到 NPE。
谢谢!
Thanks for the comments guys.
It turns out I did have a race condition. I didn't realise that onActivityResult was called before onResume, and so it turned out that on slower phones, the runnable task would be executed after onResume was called (and so the database had been opened), but on faster phones, the runnable task would get to the database operation first and be met with a NPE.
Thanks!