【发布时间】:2014-10-21 20:36:17
【问题描述】:
发生了一些奇怪的事情。我有一个 AsyncTask 正在运行,这样我就不会在 UI Thread 上获取数据库数据。这是我的任务:
ProductListActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_list);
if (savedInstanceState == null) {
new QueryProductsTask(this).execute();
}
}
public void populateProductList(Cursor products) {
ProductListAdapter productListAdapter =
new ProductListAdapter(this, R.layout.product_list_row, products);
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(productListAdapter);
}
QueryProductsTask.java:
protected Cursor doInBackground(Void... params) {
android.os.Debug.waitForDebugger();
Log.v("BagIt.AsyncTask", "doInBackground running");
// Fetches info from database:
return new Products(activity.getBaseContext()).select(null, null, null, null);
}
protected void onPostExecute(Cursor c) {
android.os.Debug.waitForDebugger();
Log.v("BagIt.AsyncTask", "onPostExecute running");
// Populates ListView through a custom adapter
activity.populateProductList(c);
}
当我处于调试模式时,此代码有效。据我目前所知,Adapter 或 Db 类没有问题。但是当我在手机上打开应用程序,或者点击“运行”而不是“调试”时,它不起作用。即使我在没有任何断点的情况下进行调试,它也会运行,但不会在调试模式之外运行。
这是我的日志:
调试模式
10-21 23:35:05.988 15665-15665/com.chenasraf.bagit W/Xposed﹕ Package name for /data/app/com.ugglynoodle.overflowmod-1.apk had to be retrieved via parser
10-21 23:35:06.258 15665-15665/com.chenasraf.bagit W/ActivityThread﹕ Application com.chenasraf.bagit is waiting for the debugger on port 8100...
10-21 23:35:06.328 15665-15665/com.chenasraf.bagit I/System.out﹕ Sending WAIT chunk
10-21 23:35:06.328 15665-15671/com.chenasraf.bagit I/dalvikvm﹕ Debugger is active
10-21 23:35:06.528 15665-15665/com.chenasraf.bagit I/System.out﹕ Debugger has connected
10-21 23:35:06.528 15665-15665/com.chenasraf.bagit I/System.out﹕ waiting for debugger to settle...
10-21 23:35:06.728 15665-15665/com.chenasraf.bagit I/System.out﹕ waiting for debugger to settle...
10-21 23:35:06.929 15665-15665/com.chenasraf.bagit I/System.out﹕ waiting for debugger to settle...
10-21 23:35:07.129 15665-15665/com.chenasraf.bagit I/System.out﹕ waiting for debugger to settle...
10-21 23:35:07.329 15665-15665/com.chenasraf.bagit I/System.out﹕ waiting for debugger to settle...
10-21 23:35:07.529 15665-15665/com.chenasraf.bagit I/System.out﹕ waiting for debugger to settle...
10-21 23:35:07.729 15665-15665/com.chenasraf.bagit I/System.out﹕ waiting for debugger to settle...
10-21 23:35:07.930 15665-15665/com.chenasraf.bagit I/System.out﹕ waiting for debugger to settle...
10-21 23:35:08.130 15665-15665/com.chenasraf.bagit I/System.out﹕ waiting for debugger to settle...
10-21 23:35:08.330 15665-15665/com.chenasraf.bagit I/System.out﹕ debugger has settled (1400)
10-21 23:35:08.510 15665-15847/com.chenasraf.bagit V/BagIt.AsyncTask﹕ doInBackground running
10-21 23:35:08.630 15665-15665/com.chenasraf.bagit I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
10-21 23:35:08.731 15665-15665/com.chenasraf.bagit D/OpenGLRenderer﹕ Enabling debug mode 0
10-21 23:35:08.881 15665-15665/com.chenasraf.bagit V/BagIt.AsyncTask﹕ onPostExecute running
10-21 23:35:09.101 15665-15665/com.chenasraf.bagit W/InputMethodManager﹕ Ignoring onBind: cur seq=4640, given seq=4639
如您所见,Log.v()s 工作正常(最后几行)
运行模式
10-21 23:30:29.545 15022-15022/com.chenasraf.bagit W/Xposed﹕ Package name for /data/app/com.ugglynoodle.overflowmod-1.apk had to be retrieved via parser
10-21 23:30:29.585 15022-15043/com.chenasraf.bagit I/System.out﹕ Sending WAIT chunk
10-21 23:30:29.625 15022-15022/com.chenasraf.bagit I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
10-21 23:30:29.655 15022-15022/com.chenasraf.bagit D/OpenGLRenderer﹕ Enabling debug mode 0
【问题讨论】:
-
删除
android.os.Debug.waitForDebugger(); -
我认为它只能让我调试,因为它是一个同步任务。为什么它必须等待调试器或根本不运行?
-
因为它仅用于调试目的。而且它是异步的而不是“同步的”......相反。
-
错字:P对不起。感谢您的答复,我会尽快接受您的答复
-
好的,我以为这可能是个意外,但想确保你理解
标签: java android android-asynctask