android:onPostExecute没有在AsyncTask上调用

时间:2015-01-15 17:52:50

标签: android android-asynctask

我已尝试过在SO上发布的类似问题提供的各种解决方案,但没有运气。

从我的活动RestCus我调用FetchDetails:

public class RestCus extends Activity{ 
    public void onCreate(Bundle savedInstanceState) {
      new FetchDetails().execute();
      super.onCreate(savedInstanceState);

      setContentView(R.layout.user_add);
      DatabaseHandler db = new DatabaseHandler(getApplicationContext());
      HashMap<String, Object> user = new HashMap<String, Object>();
      user = db.getSelectedUserDetails();

FetchDetails用于在sqlite数据库表中插入数据:

private class FetchDetails extends AsyncTask<String, String, JSONObject> {
    String uid; 
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Intent intent = getIntent();
        uid = intent.getStringExtra("user");
     }

    protected JSONObject doInBackground(String... args) {
        UserFunctions userFunction = new UserFunctions();
        JSONObject json = userFunction.FetchUser(uid);
        return json;
    }

    protected void onPostExecute(JSONObject json) {
         try {
           if (json.getString(KEY_SUCCESS) != null) {
                String res = json.getString(KEY_SUCCESS);
                if(Integer.parseInt(res) == 1){
                    DatabaseHandler db_fetch = new DatabaseHandler(getApplicationContext());
                    JSONObject json_user = json.getJSONObject("user");
                    db_fetch.selectedUser(json_user.getString(KEY_UID),json_user.getString(KEY_MOBILE),json_user.getString(KEY_USERNAME),json_user.getString(KEY_AREA),json_user.getString(KEY_CITY),json_user.getString(KEY_EMAIL));
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
   }        

在调试时我发现,一旦doInBackground()完成并返回json,程序而不是执行onPostExecute()会返回到setContentView行的RestCus活动。

我在多个活动中使用过AsyncTask,一切正常。我无法辨别我的错误。

完整的logcat如下:

01-15 12:25:29.780: E/SQLiteLog(2818): (1) no such table: user_select
01-15 12:25:29.790: D/AndroidRuntime(2818): Shutting down VM
01-15 12:25:29.790: W/dalvikvm(2818): threadid=1: thread exiting with uncaught exception (group=0xb1d6eb20)
01-15 12:25:29.810: E/AndroidRuntime(2818): FATAL EXCEPTION: main
01-15 12:25:29.810: E/AndroidRuntime(2818): Process: com.example.cresto, PID: 2818
01-15 12:25:29.810: E/AndroidRuntime(2818): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cresto/com.example.cresto.userToDet}: android.database.sqlite.SQLiteException: no such table: user_select (code 1): , while compiling: SELECT * FROM user_select
01-15 12:25:29.810: E/AndroidRuntime(2818):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at android.os.Looper.loop(Looper.java:136)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at android.app.ActivityThread.main(ActivityThread.java:5017)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at java.lang.reflect.Method.invokeNative(Native Method)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at java.lang.reflect.Method.invoke(Method.java:515)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at dalvik.system.NativeStart.main(Native Method)
01-15 12:25:29.810: E/AndroidRuntime(2818): Caused by: android.database.sqlite.SQLiteException: no such table: user_select (code 1): , while compiling: SELECT * FROM user_select
01-15 12:25:29.810: E/AndroidRuntime(2818):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at com.example.cresto.library.DatabaseHandleruserLoad.getSelecteduserDetails(DatabaseHandleruserLoad.java:95)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at com.example.cresto.userToDet.onCreate(userToDet.java:50)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at android.app.Activity.performCreate(Activity.java:5231)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-15 12:25:29.810: E/AndroidRuntime(2818):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-15 12:25:29.810: E/AndroidRuntime(2818):     ... 11 more
01-15 12:25:29.940: E/JSON(2818): {"tag":"fetchuser","success":1,"error":0,"user":{"mobile":"5089835","city":"LA","area":"Sector Sigma","uname":"udupy","uid":"udupy.5089","email":"udupy.5089@gmail.com"}}

user_select是我试图在我的onPostExecute()

中填充的sqlite数据库表

2 个答案:

答案 0 :(得分:0)

你的Sqlite dB缺少表..修复它,你的帖子执行将运行

getKey(成功)也可能返回null。添加带有日志的else条件

更新:

当主线程(onCreate)正在运行时,doInBackground并行发生。因此,在调用postExecute之前调用db.getSelectedUserDetails()并使程序崩溃而不给postExecute一个运行的机会。相反,将setContentView之后的代码放在postExecute中,即:

DatabaseHandler db = new DatabaseHandler(getApplicationContext());
      HashMap<String, Object> user = new HashMap<String, Object>();
      user = db.getSelectedUserDetails();

我很确定这是你的问题。

答案 1 :(得分:-1)

请在方法@OverrideonPostExecute上方添加doInBackground注释。