AsyncTask只适用于调试?

时间:2014-10-21 20:36:17

标签: java android android-asynctask

发生了奇怪的事情。我的AsyncTask正在运行,因此我无法在用户界面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类没有错。但是,当我在手机上打开应用程序,或点击"运行"而不是"调试",它不起作用。即使我在调试时没有任何断点,它也会运行,但不会在调试模式之外。

这是我的logcat:

调试模式

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()正常工作(最后几行)

运行模式

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

3 个答案:

答案 0 :(得分:5)

来自the docs

  

等到调试器附加。

删除该行将允许它在没有连接调试器时运行。

答案 1 :(得分:3)

您的代码明确等待调试器附加。由于您没有在调试模式下运行代码,waitForDebugger()永远不会返回。

The documentation for waitForDebugger()读到:

  

等到调试器附加。一旦调试器附加,就会返回

简单地说,删除该逻辑。在调试模式下启动应用程序将等待调试器自动附加。

答案 2 :(得分:1)

删除它,或者如果你在调试模式下运行则等待和记录的块。

protected Cursor doInBackground(Void... params) {
    if (BuildConfig.DEBUG){
        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) {
    if (BuildConfig.DEBUG){
        android.os.Debug.waitForDebugger();
        Log.v("BagIt.AsyncTask", "onPostExecute running");
    }
    // Populates ListView through a custom adapter
    activity.populateProductList(c);
}

您需要向您的类添加import语句,BuildConfig应该与您的应用程序或库模块具有相同的包名称