-1

I have tried various solutions provided on similar questions posted on SO but with no luck.

From my activity RestCus I am calling 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 is used to insert data in an sqlite database table:

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

On debugging I observe that once doInBackground() completes and returns json, the program instead of executing onPostExecute() goes back to RestCus activity at setContentView line.

I have used AsyncTask in multiple activities and all work fine. I am unable to identify my mistake.

The complete logcat is as follows:

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 is the sqlite DB table that I am trying to populate in my onPostExecute()

Puneet7nov
  • 32
  • 6

2 Answers2

0

Your Sqlite dB is missing table.. Fix it and your post execute will run

The getKey(success) could be returning null too. Add else condition with logs in it

UPDATE:

The doInBackground is happening in parallel while the main thread (onCreate) is running. Therefore, the db.getSelectedUserDetails() is being called before postExecute is being called and crashing the program not giving the postExecute a chance to run . Instead, put the code that is after setContentView , in the postExecute.. namely :

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

I am pretty sure that is your problem.

Snake
  • 14,228
  • 27
  • 117
  • 250
  • That table is created and populated in onPostExecute() but the program does not reach there instead it goes back to the main activity and there it tries to select data from the Sqlite DB table (which is not created) in db.getSelectedUserDetails(). Therefore the error message "no such table" – Puneet7nov Jan 15 '15 at 19:18
  • See updated answer. If it solves your problem .. accept the answer plz as your profile shows no accepted answers at all. Maybe you don't know that because you are new here, but this will deter people from answering:) – Snake Jan 15 '15 at 20:23
  • Snake, you seem to have identified the problem (I observed the same flow while debugging). I will give it a shot and confirm the answer once I am near my laptop. P.S. - Yes I am fairly new here and with Android but if you closely watch my threads there are either questions unanswered or I solved my problem on my own and left my answer. However, I will surely keep in mind to follow your advise to accept the answers :) – Puneet7nov Jan 16 '15 at 07:15
  • Thanks, confirm it and report back :) Even if you solve it yourself, put your answer and accept it. It gives you points and it helps others who face the same thing – Snake Jan 16 '15 at 15:09
  • That worked !!!! Thank you for the help. I have accepted your answer :) I will surely follow your advise going forward. :) – Puneet7nov Jan 16 '15 at 16:21
-1

Please add the @Overrideannotations above your methods onPostExecute and doInBackground.

Kody
  • 1,154
  • 3
  • 14
  • 31
  • 2
    It's irrelevant to the problem he is having and therefore is not actually a valid answer. – James McCracken Jan 15 '15 at 18:11
  • 1
    Because this won't fix anything. I think you might not understand what the `Override` annotation does. http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why – Krylez Jan 15 '15 at 18:12