Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8402489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:04:58+00:00 2026-06-09T22:04:58+00:00

After i implement my login codes into the async task doInBackground method, my application

  • 0

After i implement my login codes into the async task doInBackground method, my application crashes. I needed this help because if i dont implement async task for login, my application in the real device will crash due to policy issue. Can i konw where my errors is?

Below is my doInBackground task:

// Checking login in background
    protected String doInBackground(String... params) {
        String email = inputEmail.getText().toString();
        String password = inputPassword.getText().toString();
        UserFunctions userFunction = new UserFunctions();
        JSONObject json = userFunction.loginUser(email, password);

        // check for login response
        try {
            if (json.getString(KEY_SUCCESS) != null) {
                loginErrorMsg.setText("");
                String res = json.getString(KEY_SUCCESS);
                if (Integer.parseInt(res) == 1) {
                    // user successfully logged in
                    // Store user details in SQLite Database
                    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                    JSONObject json_user = json.getJSONObject("user");

                    // Clear all previous data in database
                    userFunction.logoutUser(getApplicationContext());
                    db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));

                    // Launch HomePage Screen
                    Intent homepage = new Intent(getApplicationContext(), HomepageActivity.class);

                    // Close all views before launching HomePage
                    homepage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(homepage);

                    // Close Login Screen
                    finish();
                } else {
                    // Error in login
                    loginErrorMsg.setText("Incorrect username/password");
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

And here is my logcat:

08-18 22:34:59.088: E/JSON(382): {"tag":"login","success":1,"error":0,"uid":"502cfeb2e60004.54904838","user":{"name":"user1","email":"user1@gmail.com","created_at":"2012-08-16 22:07:46","updated_at":null}}
08-18 22:34:59.137: E/AndroidRuntime(382): FATAL EXCEPTION: AsyncTask #1
08-18 22:34:59.137: E/AndroidRuntime(382): java.lang.RuntimeException: An error occured while executing doInBackground()
08-18 22:34:59.137: E/AndroidRuntime(382):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-18 22:34:59.137: E/AndroidRuntime(382):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-18 22:34:59.137: E/AndroidRuntime(382):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-18 22:34:59.137: E/AndroidRuntime(382):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-18 22:34:59.137: E/AndroidRuntime(382):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-18 22:34:59.137: E/AndroidRuntime(382):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
08-18 22:34:59.137: E/AndroidRuntime(382):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
08-18 22:34:59.137: E/AndroidRuntime(382):  at java.lang.Thread.run(Thread.java:1096)
08-18 22:34:59.137: E/AndroidRuntime(382): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-18 22:34:59.137: E/AndroidRuntime(382):  at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
08-18 22:34:59.137: E/AndroidRuntime(382):  at android.view.ViewRoot.invalidateChild(ViewRoot.java:607)
08-18 22:34:59.137: E/AndroidRuntime(382):  at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633)
08-18 22:34:59.137: E/AndroidRuntime(382):  at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505)
08-18 22:34:59.137: E/AndroidRuntime(382):  at android.view.View.invalidate(View.java:5139)
08-18 22:34:59.137: E/AndroidRuntime(382):  at android.widget.TextView.checkForRelayout(TextView.java:5364)
08-18 22:34:59.137: E/AndroidRuntime(382):  at android.widget.TextView.setText(TextView.java:2688)
08-18 22:34:59.137: E/AndroidRuntime(382):  at android.widget.TextView.setText(TextView.java:2556)
08-18 22:34:59.137: E/AndroidRuntime(382):  at android.widget.TextView.setText(TextView.java:2531)
08-18 22:34:59.137: E/AndroidRuntime(382):  at com.stts.sparetimetradingsystem.LoginActivity$Login.doInBackground(LoginActivity.java:119)
08-18 22:34:59.137: E/AndroidRuntime(382):  at com.stts.sparetimetradingsystem.LoginActivity$Login.doInBackground(LoginActivity.java:1)
08-18 22:34:59.137: E/AndroidRuntime(382):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-18 22:34:59.137: E/AndroidRuntime(382):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-18 22:34:59.137: E/AndroidRuntime(382):  ... 4 more
08-18 22:35:01.867: E/WindowManager(382): Activity com.stts.sparetimetradingsystem.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f0a510 that was originally added here
08-18 22:35:01.867: E/WindowManager(382): android.view.WindowLeaked: Activity com.stts.sparetimetradingsystem.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f0a510 that was originally added here
08-18 22:35:01.867: E/WindowManager(382):   at android.view.ViewRoot.<init>(ViewRoot.java:247)
08-18 22:35:01.867: E/WindowManager(382):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
08-18 22:35:01.867: E/WindowManager(382):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-18 22:35:01.867: E/WindowManager(382):   at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-18 22:35:01.867: E/WindowManager(382):   at android.app.Dialog.show(Dialog.java:241)
08-18 22:35:01.867: E/WindowManager(382):   at com.stts.sparetimetradingsystem.LoginActivity$Login.onPreExecute(LoginActivity.java:106)
08-18 22:35:01.867: E/WindowManager(382):   at android.os.AsyncTask.execute(AsyncTask.java:391)
08-18 22:35:01.867: E/WindowManager(382):   at com.stts.sparetimetradingsystem.LoginActivity$1.onClick(LoginActivity.java:60)
08-18 22:35:01.867: E/WindowManager(382):   at android.view.View.performClick(View.java:2408)
08-18 22:35:01.867: E/WindowManager(382):   at android.view.View.onKeyUp(View.java:4121)
08-18 22:35:01.867: E/WindowManager(382):   at android.widget.TextView.onKeyUp(TextView.java:4431)
08-18 22:35:01.867: E/WindowManager(382):   at android.view.KeyEvent.dispatch(KeyEvent.java:1061)
08-18 22:35:01.867: E/WindowManager(382):   at android.view.View.dispatchKeyEvent(View.java:3740)
08-18 22:35:01.867: E/WindowManager(382):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
08-18 22:35:01.867: E/WindowManager(382):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
08-18 22:35:01.867: E/WindowManager(382):   at android.widget.ScrollView.dispatchKeyEvent(ScrollView.java:318)
08-18 22:35:01.867: E/WindowManager(382):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
08-18 22:35:01.867: E/WindowManager(382):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
08-18 22:35:01.867: E/WindowManager(382):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
08-18 22:35:01.867: E/WindowManager(382):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1667)
08-18 22:35:01.867: E/WindowManager(382):   at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1102)
08-18 22:35:01.867: E/WindowManager(382):   at android.app.Activity.dispatchKeyEvent(Activity.java:2063)
08-18 22:35:01.867: E/WindowManager(382):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1643)
08-18 22:35:01.867: E/WindowManager(382):   at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2471)
08-18 22:35:01.867: E/WindowManager(382):   at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2441)
08-18 22:35:01.867: E/WindowManager(382):   at android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
08-18 22:35:01.867: E/WindowManager(382):   at android.os.Handler.dispatchMessage(Handler.java:99)
08-18 22:35:01.867: E/WindowManager(382):   at android.os.Looper.loop(Looper.java:123)
08-18 22:35:01.867: E/WindowManager(382):   at android.app.ActivityThread.main(ActivityThread.java:4627)
08-18 22:35:01.867: E/WindowManager(382):   at java.lang.reflect.Method.invokeNative(Native Method)
08-18 22:35:01.867: E/WindowManager(382):   at java.lang.reflect.Method.invoke(Method.java:521)
08-18 22:35:01.867: E/WindowManager(382):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-18 22:35:01.867: E/WindowManager(382):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-18 22:35:01.867: E/WindowManager(382):   at dalvik.system.NativeStart.main(Native Method)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:05:00+00:00Added an answer on June 9, 2026 at 10:05 pm

    You are updating the view in the doInBackground function which happens in the background thread. View should always be updated in the UI thread. To solve this, you can return the JSONObject from the doInBackground function and do the processing in the onPostExecute method.

    Move the try block after userFunction.loginUser(email, password); to onPostExecute.

    protected String doInBackground(String... params) {
        String email = inputEmail.getText().toString();
        String password = inputPassword.getText().toString();
        UserFunctions userFunction = new UserFunctions();
        JSONObject json = userFunction.loginUser(email, password);
    
        return json;
    }
    
    protected void onPostExecute(JSONObject json) {
    
        // check for login response
        try {
            if (json.getString(KEY_SUCCESS) != null) {
                loginErrorMsg.setText("");
                String res = json.getString(KEY_SUCCESS);
                if (Integer.parseInt(res) == 1) {
                    // user successfully logged in
                    // Store user details in SQLite Database
                    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                    JSONObject json_user = json.getJSONObject("user");
    
                    // Clear all previous data in database
                    userFunction.logoutUser(getApplicationContext());
                    db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
    
                    // Launch HomePage Screen
                    Intent homepage = new Intent(getApplicationContext(), HomepageActivity.class);
    
                    // Close all views before launching HomePage
                    homepage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(homepage);
    
                    // Close Login Screen
                    finish();
                } else {
                    // Error in login
                    loginErrorMsg.setText("Incorrect username/password");
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After discovering about Javascript namespaces, I tried to implement them but I run into
I need to implement Android application which will be expired after few days or
I'm trying to implement ZXing barcode scanner into my program. After getting the scanned
I've been trying to implement the Facebook SDK into my application in order to
I am trying to implement login method with JSF, EJB and JPA. My current
After reading What is the best way to implement nested dictionaries? why is it
I would like to implement operations that is undoable in a short time after
I am trying to implement some code on my web page to auto-scroll after
I am using jcrop to implement image cropping in my site. After clicking on
I need to implement a very specific demo mechanism. It must not expire after

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.