【问题标题】:Android AsyncTask not working without execute().get()Android AsyncTask 在没有 execute().get() 的情况下无法工作
【发布时间】:2014-04-04 13:07:33
【问题描述】:

我正在尝试使用 AsyncTasks 插入我的数据库。 当我打电话时:

    new insert(tableName).execute(val).get;

它按预期工作,但它阻塞了 UI 线程。

我宁愿打电话

    new insert(tableName).execute(val);

但是当我这样做时,doInBackground 会引发 SQL 错误,并且我传入的 ContentValues 是空的。 这是我的 AsyncTask:

            private class insert extends AsyncTask<ContentValues, Object, Long>{

        String inTable;
        long id = -1;
        insert(TableNames table){
            inTable = table.getSQL(); //This simple gets the String value TableName
        }
        @Override
        protected void onPostExecute(Long result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
                Log.i(tag, "PostExecute: "+ inTable + " " + String.valueOf(id));    
        }

        @Override
        protected Long doInBackground(ContentValues... values) {
            try {
                id = getWritableDatabase().insert(inTable, null, values[0]);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return id;
        }

    }

在这种情况下,我不关心他的返回值。插入物是火,忘记它。

原木猫:

Error inserting 
 android.database.sqlite.SQLiteException: near "null": syntax error (code 1): , while compiling: INSERT INTO TrackingLog(null) VALUES (NULL)
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
    at com.westat.nopus.util.data.DataHelper$insert.doInBackground(DataHelper.java:563)
    at com.westat.nopus.util.data.DataHelper$insert.doInBackground(DataHelper.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:288)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:841)
 Error inserting 
 android.database.sqlite.SQLiteException: near "null": syntax error (code 1): , while compiling: INSERT INTO TrackingLog(null) VALUES (NULL)
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
    at com.westat.nopus.util.data.DataHelper$insert.doInBackground(DataHelper.java:563)
    at com.westat.nopus.util.data.DataHelper$insert.doInBackground(DataHelper.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:288)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

调用AsyncTask的完整方法:

         void Tableinsert(TableNames tableName,ContentValues val){

     val.put("Exported_Flag", "F");

    if (debug) {
        Log.i(tag, "TableInsert.  Size: " + String.valueOf(val.size()));
        //This returns 26 - Which is the correct size of the ContentValues
    }
    try {
        new insert(tableName).execute(val); //Doesn't work
    //  new insert(tableName).execute(val).get; //Works
    } 
    catch (Exception e) {
        if (MyApplication.SEND_HANDLED_EXCEPTIONS) {
            ACRA.getErrorReporter().handleSilentException(e);
        }
        e.printStackTrace();
    }
}

【问题讨论】:

  • 你是如何调用异步任务的。您必须在调用 asynctask 时传递内容值。
  • @Navaguesser LogCat 中有什么吗??
  • 您是在此之后立即完成您的Activity 还是暂停它?
  • @Shrimam - 抱歉...我一直按回车键。像这样调用任务: new insert(tableName,false).execute(val);
  • @codeMagic 实际上在这种情况下没有活动。它是从服务中调用的。它是一个 GPS 跟踪日志,仅作为服务运行。但是,从 Activity 插入时我遇到了同样的问题,是的,当您获得调用 asynctask 的按钮时,它就完成了

标签: android sqlite android-asynctask


【解决方案1】:

谢谢大家,不过看了cmets后,我发现了我的问题。

当我创建内容值、值并将它们传递给 AsyncTask 时,主线程上的下一行是 values.clear()。显然,这会在 AsyncTask 将它们插入数据库之前清空这些值。这解释了为什么它使用 execute().get()。

再次感谢...

【讨论】:

    【解决方案2】:

    在标题中

     private class insert extends AsyncTask<ContentValues, Object, Long>{ ...
    

    Void替换Long,对

    做同样的事情
    protected Long doInBackground(ContentValues... values) {
    

    也是。 现在你不再需要返回值了。

    【讨论】:

    • 这不是试图解决问题,而是关于 doInBackground() 方法中的变量值内容。
    猜你喜欢
    • 2012-09-10
    • 2012-04-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多