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

  • SEARCH
  • Home
  • 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 8429371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:19:05+00:00 2026-06-10T05:19:05+00:00

I am doing a login application ( taken from here ) and basically, i

  • 0

I am doing a login application (taken from here) and basically, i want login using the nric and password which will connect to a online database, however there the program crash after i added the async task into the codes.

public class LoginActivity extends Activity {
Button btnLogin;
Button btnLinkToRegister;
EditText inputNric;
EditText inputPassword;
TextView loginErrorMsg;

// Progress Dialog
private ProgressDialog pDialog;

// JSON Response node names
//success is the column name of the database - KEY_SUCCESS is we create the name 
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_NAME = "name";
private static String KEY_NRIC = "nric";
private static String KEY_CREATED_AT = "created_at";

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    // Importing all assets like buttons, text fields
    inputNric = (EditText) findViewById(R.id.loginNric);
    inputPassword = (EditText) findViewById(R.id.loginPassword);
    btnLogin = (Button) findViewById(R.id.buttonLogin);
    btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
    loginErrorMsg = (TextView) findViewById(R.id.login_error);

    // login button click event
    btnLogin.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // starting background task to update case
            new LoginUser().execute();
        }
    });

    // Link to Register Screen
    btnLinkToRegister.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent i = new Intent(getApplicationContext(),
            RegisterActivity.class);
            startActivity(i);
            finish();
        }
    });
}

/**
 * Background Async Task to  Login User
 * */
class LoginUser extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */        
    protected void onPreExecute() {
    super.onPreExecute();
    pDialog = new ProgressDialog(LoginActivity.this);
    pDialog.setMessage("Logging In ...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(true);
    pDialog.show();
    }

    /**
    * Logging in
    * */
    protected String doInBackground(String... params) {

            String nric = inputNric.getText().toString();
            String password = inputPassword.getText().toString();
            UserFunctions userFunction = new UserFunctions();
            Log.d("Button", "Login");
            JSONObject json = userFunction.loginUser(nric, 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_NRIC), json_user.getString(KEY_CREATED_AT));                      

                        // Launch home Screen
                        Intent home = new Intent(getApplicationContext(), HomeActivity.class);

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

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

     /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once case deleted
        pDialog.dismiss();

    }
}

}

Logcat Errors:

08-15 06:20:26.254: E/AndroidRuntime(302): FATAL EXCEPTION: AsyncTask #1
08-15 06:20:26.254: E/AndroidRuntime(302): java.lang.RuntimeException: An error occured      while executing doInBackground()
08-15 06:20:26.254: E/AndroidRuntime(302):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-15 06:20:26.254: E/AndroidRuntime(302):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-15 06:20:26.254: E/AndroidRuntime(302):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-15 06:20:26.254: E/AndroidRuntime(302):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-15 06:20:26.254: E/AndroidRuntime(302):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-15 06:20:26.254: E/AndroidRuntime(302):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
08-15 06:20:26.254: E/AndroidRuntime(302):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
08-15 06:20:26.254: E/AndroidRuntime(302):  at java.lang.Thread.run(Thread.java:1096)
08-15 06:20:26.254: E/AndroidRuntime(302): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-15 06:20:26.254: E/AndroidRuntime(302):  at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
08-15 06:20:26.254: E/AndroidRuntime(302):  at      android.view.ViewRoot.requestLayout(ViewRoot.java:594)
08-15 06:20:26.254: E/AndroidRuntime(302):  at android.view.View.requestLayout(View.java:8125)
08-15 06:20:26.254: E/AndroidRuntime(302):  at android.view.View.requestLayout(View.java:8125)
08-15 06:20:26.254: E/AndroidRuntime(302):  at android.view.View.requestLayout(View.java:8125)
08-15 06:20:26.254: E/AndroidRuntime(302):  at android.view.View.requestLayout(View.java:8125)
08-15 06:20:26.254: E/AndroidRuntime(302):  at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:254)
08-15 06:20:26.254: E/AndroidRuntime(302):  at android.view.View.requestLayout(View.java:8125)
08-15 06:20:26.254: E/AndroidRuntime(302):  at android.widget.TextView.checkForRelayout(TextView.java:5378)
08-15 06:20:26.254: E/AndroidRuntime(302):  at android.widget.TextView.setText(TextView.java:2688)
08-15 06:20:26.254: E/AndroidRuntime(302):  at android.widget.TextView.setText(TextView.java:2556)
08-15 06:20:26.254: E/AndroidRuntime(302):  at android.widget.TextView.setText(TextView.java:2531)
08-15 06:20:26.254: E/AndroidRuntime(302):  at com.pivestigator.LoginActivity$LoginUser.doInBackground(LoginActivity.java:107)
08-15 06:20:26.254: E/AndroidRuntime(302):  at com.pivestigator.LoginActivity$LoginUser.doInBackground(LoginActivity.java:1)
08-15 06:20:26.254: E/AndroidRuntime(302):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-15 06:20:26.254: E/AndroidRuntime(302):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-15 06:20:26.254: E/AndroidRuntime(302):  ... 4 more
08-15 06:20:26.344: W/ActivityManager(75):   Force finishing activity com.pivestigator/.LoginActivity
08-15 06:20:28.554: E/WindowManager(302): Activity com.pivestigator.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f33188 that was originally added here
08-15 06:20:28.554: E/WindowManager(302): android.view.WindowLeaked: Activity com.pivestigator.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f33188 that was originally added here
08-15 06:20:28.554: E/WindowManager(302):   at android.view.ViewRoot.<init>(ViewRoot.java:247)
08-15 06:20:28.554: E/WindowManager(302):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
08-15 06:20:28.554: E/WindowManager(302):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-15 06:20:28.554: E/WindowManager(302):   at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-15 06:20:28.554: E/WindowManager(302):   at android.app.Dialog.show(Dialog.java:241)
08-15 06:20:28.554: E/WindowManager(302):   at com.pivestigator.LoginActivity$LoginUser.onPreExecute(LoginActivity.java:90)
08-15 06:20:28.554: E/WindowManager(302):   at android.os.AsyncTask.execute(AsyncTask.java:391)
08-15 06:20:28.554: E/WindowManager(302):   at com.pivestigator.LoginActivity$1.onClick(LoginActivity.java:60)
08-15 06:20:28.554: E/WindowManager(302):   at android.view.View.performClick(View.java:2408)
08-15 06:20:28.554: E/WindowManager(302):   at android.view.View$PerformClick.run(View.java:8816)
08-15 06:20:28.554: E/WindowManager(302):   at android.os.Handler.handleCallback(Handler.java:587)
08-15 06:20:28.554: E/WindowManager(302):   at android.os.Handler.dispatchMessage(Handler.java:92)
08-15 06:20:28.554: E/WindowManager(302):   at android.os.Looper.loop(Looper.java:123)
08-15 06:20:28.554: E/WindowManager(302):   at android.app.ActivityThread.main(ActivityThread.java:4627)
08-15 06:20:28.554: E/WindowManager(302):   at java.lang.reflect.Method.invokeNative(Native Method)
08-15 06:20:28.554: E/WindowManager(302):   at java.lang.reflect.Method.invoke(Method.java:521)
08-15 06:20:28.554: E/WindowManager(302):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-15 06:20:28.554: E/WindowManager(302):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-15 06:20:28.554: E/WindowManager(302):   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-10T05:19:07+00:00Added an answer on June 10, 2026 at 5:19 am

    Move loginErrorMsg.setText("Incorrect username/password"); to the AsyncTask's onProgressUpdate(..) method, and call publishProgress(..) instead in the doInBackground() thread.

    The other problem is (wich in my opinion caused the crash) is that you are trying to start a new Activity from the worker thread. Move the following code to the onPostExecute(..) method, just after pDialog.dismiss() and check there if the login was successful through the doInBackground() return value, and the argument of onPostExecute().

    The leaked window exception means, that the ProgressDialog is still visible, when you try to finish your acttivity.

    class LoginUser extends AsyncTask<Void, String, Boolean> {
    
        /**
         * Before starting background thread Show Progress Dialog
         * */        
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(LoginActivity.this);
            pDialog.setMessage("Logging In ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
    
        /**
        * Logging in
        * */
        protected Boolean doInBackground(Void... params) {
    
            String nric = inputNric.getText().toString();
            String password = inputPassword.getText().toString();
            UserFunctions userFunction = new UserFunctions();
            Log.d("Button", "Login");
            JSONObject json = userFunction.loginUser(nric, password);
    
            boolean isSuccess = false;
    
            // check for login response
            try {
                if (json.getString(KEY_SUCCESS) != null) {
                    // Set the error TextField on the UI thread
                    publishProgress("");
    
                    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_NRIC), json_user.getString(KEY_CREATED_AT));                      
    
    
                        // We logged in successfully
                        isSuccess = true;
                    }else{
                        // Error in login
                        // Set the error TextField on the UI thread
                        publishProgress("Incorrect username/password");
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return isSuccess;
        }
    
        @Override
        protected void onProgressUpdate(String... values) {
            loginErrorMsg.setText(values[0]);
        }
    
         /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(Boolean result) {
            // dismiss the dialog once case deleted
            pDialog.dismiss();
    
            if(result.booleanValue()){
                // Launch home Screen
                    Intent home = new Intent(getApplicationContext(), HomeActivity.class);
    
                    // Close all views before launching home
                    home.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(home);
    
                    // Close Login Screen
                    finish();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an application using Sencha Touch that will require a login to
I am doing a application that deals with where Users can login and see
i am doing one small application , wheich have login functionality, in the user
for the page I am doing needs a login authentication using Twitter (using tweetphp
Having watched samples from Rob Conery's Kona application, I see that he is using
I am using Context.RewritePath() in ASP.NET 3.5 application running on IIS7. I am doing
I have a web application which uses LDAP for log in. I'm using Openid4Java
In my web application i want users to login with their facebook account. I
I am integrating facebook connect with my application which uses authlogic, facebooker, and the
I'm using a flash component in my symfony2 application which uploads multiple images, and

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.