【问题标题】:AsyncTask only works on debug?AsyncTask 仅适用于调试?
【发布时间】: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


【解决方案1】:

来自the docs

等待调试器附加。

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

【讨论】:

    【解决方案2】:

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

    The documentation for waitForDebugger() 读作:

    等到调试器附加。一旦调试器附加,这将返回

    简单地说,去掉那个逻辑。以调试模式启动您的应用将等待调试器自动附加。

    【讨论】:

      【解决方案3】:

      删除它或添加这个 if 块,如果您在调试模式下运行,它将等待并记录。

      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);
      }
      

      您需要在您的类中添加一个导入语句,BuildConfig 应该与您的应用或库模块具有相同的包名称

      【讨论】:

      • 我还建议不要使用BuildConfig.DEBUG,因为它本身就被破坏了,并且更喜欢在 Gradle 中定义自己的 DEBUG 参数。
      • 如何在 Gradle 中定义调试参数并在代码中访问它?
      猜你喜欢
      • 2015-11-16
      • 2018-04-20
      • 2017-07-07
      • 2022-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-31
      • 1970-01-01
      相关资源
      最近更新 更多