-2

I am new in android application development when I am pressing on login button my application is crash and gives me an Fatal Exception please help me out.

And also I'd like to use ENTER key on softkeyboard instead of Login Button

Login class

public class Login extends Activity implements OnClickListener {

private ProgressDialog pDialog;

 Button btnLogin;
    Button btnLinkToRegister;
    EditText inputEmail;
    EditText inputPassword;
    TextView loginErrorMsg;

    // JSON Response node names


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

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        // Importing all assets like buttons, text fields
        inputEmail = (EditText) findViewById(R.id.loginEmail);
        inputPassword = (EditText) findViewById(R.id.loginPassword);
        btnLogin = (Button) findViewById(R.id.btnLogin);

        btnLogin.setOnClickListener(this);

        loginErrorMsg = (TextView) findViewById(R.id.login_error);

        // Login button Click Event
    }


    public void onClick(View v)
    {


        if(v.getId()==R.id.btnLogin)
        {
            new LoginExecute().execute();

        }
    }
    class LoginExecute extends AsyncTask<String, String, String>
    {
         String email = inputEmail.getText().toString();
         String password = inputPassword.getText().toString();
         UserFunctions userFunction = new UserFunctions();
         JSONObject json = userFunction.loginUser(email, password);


        protected String doInBackground(String... args)
        {
             try {
                 if(email.trim().length()>0 || password.trim().length()>0)
                 {

                     if (json.getString("STATUS") != null) {
                         loginErrorMsg.setText("");
                         String res3 = json.getString("STATUS");
                         System.out.println("Status = " +res3);
                         if(Integer.parseInt(res3) == 1){

                        if (json.getString("role").equalsIgnoreCase("Inspector")) 
                        {
                            loginErrorMsg.setText("");
                            String res1 = json.getString("role"); 
                            System.out.println("Role =" +res1);
                        String area = json.getString("area");
                        String  street = json.getString("street");
                        String  user_id = json.getString("user_id");
                            if(res1.equals("Inspector"))
                            {
                                Intent dashboard = new Intent(getApplicationContext(), Inspector.class);

                                // Close all views before launching Dashboard
                                dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(dashboard);
                            //  finish();

                            }

                        }
                        else if (json.getString("role").equalsIgnoreCase("Clamper")) 
                        {
                            loginErrorMsg.setText("");
                            String res1 = json.getString("role"); 
                            ;
                            if(res1.equals("Clamper"))
                            {
                                Intent dashboard1 = new Intent(getApplicationContext(), Clamper.class);

                                // Close all views before launching Dashboard
                                dashboard1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(dashboard1);
                                //finish();

                            }

                        }
                        else if (json.getString("role").equalsIgnoreCase("Declamper")) 
                        {
                            loginErrorMsg.setText("");
                            String res22 = json.getString("role");

                            System.out.println("Role =" +res22);
                            if(res22.equals("Declamper"))
                            {
                                Intent dashboard2 = new Intent(getApplicationContext(), Declamper.class);

                                // Close all views before launching Dashboard
                                dashboard2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(dashboard2);
                                //finish();

                            }

                        }
                        else if (json.getString("role").equalsIgnoreCase("Tower")) 
                        {
                            loginErrorMsg.setText("");
                            String res8 = json.getString("role");

                            System.out.println("Role =" +res3);
                            if(res8.equals("Tower"))
                            {
                                Intent dashboard3 = new Intent(getApplicationContext(), Tower.class);

                                // Close all views before launching Dashboard
                                dashboard3.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(dashboard3);
                                //finish();

                            }

                        }

                    }else{
                        // Error in login
                        loginErrorMsg.setText("Incorrect username/password");
                    }
                }
                 }else{
                     loginErrorMsg.setText("Blank username/password");
                 }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
        protected void onPostExecute(String file_url) {
            pDialog = new ProgressDialog(Login.this);
            pDialog.setMax(25);
            pDialog.setMessage("Processing....");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
            long delayInMillis = 2000;
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    pDialog.dismiss();
                }
            }, delayInMillis);
        }
    }

log error file

08-28 09:08:13.136: W/dalvikvm(1348): threadid=11: thread exiting with uncaught exception (group=0xb2b0cba8)
08-28 09:08:13.276: D/dalvikvm(1348): GC_FOR_ALLOC freed 205K, 7% free 3875K/4156K, paused 35ms, total 35ms
08-28 09:08:13.286: E/AndroidRuntime(1348): FATAL EXCEPTION: AsyncTask #1
08-28 09:08:13.286: E/AndroidRuntime(1348): Process: com.example.testlogin, PID: 1348
08-28 09:08:13.286: E/AndroidRuntime(1348): java.lang.RuntimeException: An error occured while executing doInBackground()
08-28 09:08:13.286: E/AndroidRuntime(1348):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
08-28 09:08:13.286: E/AndroidRuntime(1348):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
08-28 09:08:13.286: E/AndroidRuntime(1348):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
08-28 09:08:13.286: E/AndroidRuntime(1348):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
08-28 09:08:13.286: E/AndroidRuntime(1348):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-28 09:08:13.286: E/AndroidRuntime(1348):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-28 09:08:13.286: E/AndroidRuntime(1348):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-28 09:08:13.286: E/AndroidRuntime(1348):     at java.lang.Thread.run(Thread.java:841)
08-28 09:08:13.286: E/AndroidRuntime(1348): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-28 09:08:13.286: E/AndroidRuntime(1348):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6094)
08-28 09:08:13.286: E/AndroidRuntime(1348):     at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:857)
08-28 09:08:13.286: E/AndroidRuntime(1348):     at android.view.ViewGroup.invalidateChild(ViewGroup.java:4320)
08-28 09:08:13.286: E/AndroidRuntime(1348):     at android.view.View.invalidate(View.java:10935)
08-28 09:08:13.286: E/AndroidRuntime(1348):     at android.view.View.invalidate(View.java:10890)
08-28 09:08:13.286: E/AndroidRuntime(1348):     at android.widget.TextView.checkForRelayout(TextView.java:6587)
08-28 09:08:13.286: E/AndroidRuntime(1348):     at android.widget.TextView.setText(TextView.java:3813)
08-28 09:08:13.286: E/AndroidRuntime(1348):     at android.widget.TextView.setText(TextView.java:3671)
08-28 09:08:13.286: E/AndroidRuntime(1348):     at android.widget.TextView.setText(TextView.java:3646)
08-28 09:08:13.286: E/AndroidRuntime(1348):     at com.example.testlogin.Login$LoginExecute.doInBackground(Login.java:185)
08-28 09:08:13.286: E/AndroidRuntime(1348):     at com.example.testlogin.Login$LoginExecute.doInBackground(Login.java:1)
08-28 09:08:13.286: E/AndroidRuntime(1348):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-28 09:08:13.286: E/AndroidRuntime(1348):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-28 09:08:13.286: E/AndroidRuntime(1348):     ... 4 more
  • then whats the solution – Harshit Agarwal Aug 28 '14 at 13:27
  • Did you even read the answer? – Apoorv Aug 28 '14 at 13:27
  • or this one: http://stackoverflow.com/questions/5161951/android-only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-vi – pomber Aug 28 '14 at 13:28
  • "And also I'd like to use ENTER key on softkeyboard instead of Login Button" >> That's an entirely different question, you need to post a different question (or better yet, look for similar questions here on SO). – 2Dee Aug 28 '14 at 14:03

2 Answers2

0

The problem is with these lines (and similar ones):

loginErrorMsg.setText("");

AsyncTask is designed to run in another thread, and you can't modify the UI from another thread (that could be very bad if it were allowed).

You need to create a Runnable and run it on the UI thread or execute that code in onPostExecute like you do the other UI elements.

Like this:

String errorText = "";

private Runnable updateErrorMsg = new Runnable() {

    @Override
    public void run() {
        loginErrorMsg.setText(errorText);
    }

};


      protected String doInBackground(String... args)
    {
         try {
             if(email.trim().length()>0 || password.trim().length()>0)
             {

                 if (json.getString("STATUS") != null) {
                     loginErrorMsg.setText("");
                     String res3 = json.getString("STATUS");
                     System.out.println("Status = " +res3);
                     if(Integer.parseInt(res3) == 1){

                    if (json.getString("role").equalsIgnoreCase("Inspector")) 
                    {
                        // change to this
                        errorText = "";
                        runOnUiThread(updateErrorMsg);
                        String res1 = json.getString("role"); 
                        System.out.println("Role =" +res1);
                    String area = json.getString("area");
                    String  street = json.getString("street");
                    String  user_id = json.getString("user_id");
                        if(res1.equals("Inspector"))
                        {
                            Intent dashboard = new Intent(getApplicationContext(), Inspector.class);

                            // Close all views before launching Dashboard
                            dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(dashboard);
                        //  finish();

                        }

                    }
                    else if (json.getString("role").equalsIgnoreCase("Clamper")) 
                    {
                        // change to this
                        errorText = "";
                        runOnUiThread(updateErrorMsg);
                        String res1 = json.getString("role"); 
                        ;
                        if(res1.equals("Clamper"))
                        {
                            Intent dashboard1 = new Intent(getApplicationContext(), Clamper.class);

                            // Close all views before launching Dashboard
                            dashboard1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(dashboard1);
                            //finish();

                        }

                    }
                    else if (json.getString("role").equalsIgnoreCase("Declamper")) 
                    {
                        loginErrorMsg.setText("");
                        String res22 = json.getString("role");

                        System.out.println("Role =" +res22);
                        if(res22.equals("Declamper"))
                        {
                            Intent dashboard2 = new Intent(getApplicationContext(), Declamper.class);

                            // Close all views before launching Dashboard
                            dashboard2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(dashboard2);
                            //finish();

                        }

                    }
                    else if (json.getString("role").equalsIgnoreCase("Tower")) 
                    {
                        // change to this
                        errorText = "";
                        runOnUiThread(updateErrorMsg);
                        String res8 = json.getString("role");

                        System.out.println("Role =" +res3);
                        if(res8.equals("Tower"))
                        {
                            Intent dashboard3 = new Intent(getApplicationContext(), Tower.class);

                            // Close all views before launching Dashboard
                            dashboard3.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(dashboard3);
                            //finish();

                        }

                    }

                }else{
                    // Error in login
                    // loginErrorMsg.setText("Incorrect username/password");
                    // change to this
                    errorText = "Incorrect username/password";
                    runOnUiThread(updateErrorMsg);
                }
            }
             }else{
                 // loginErrorMsg.setText("Blank username/password");
                 // change to this
                 errorText = "Blank username/password";
                 runOnUiThread(updateErrorMsg);
             }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
Jim
  • 10,172
  • 1
  • 27
  • 36
  • can you please provide me the source code – Harshit Agarwal Aug 28 '14 at 13:30
  • I made edits - there are other ways, this is only an example. I would probably set the error text as I've shown here, then put the error message update in onPostExecute, but I think this is a more useful example – Jim Aug 28 '14 at 15:22
  • Y its work thanks alot can u please explain me with example how to error message update in onPostExecute also please I want to learn please. – Harshit Agarwal Aug 30 '14 at 06:19
  • and one more thing I want to do in my project is that In my loginActivity when I entered wrong username and password then also progress dialoge get execute I want to stop that I want to show progress dialoge only when it is valid user is it possible to do it.. – Harshit Agarwal Aug 30 '14 at 06:21
  • if you put this line `loginErrorMsg.setText(errorText);` in `onPostExecute` and get rid of the Runnable, it will also work... – Jim Aug 30 '14 at 20:09
  • And for your second question, I'm not sure what you mean. You probably should post separately (and comment here and provide a link if you like) – Jim Aug 30 '14 at 20:10
0

You cannot call setText on a TextView inside doInBackground, you should update your UI inside onPostExecute once the task is done executing, or use a Thread to change the UI during the task execution (not recommended).

If you need to show progress, you can use onProgressUpdate and publishProgress.

2Dee
  • 8,609
  • 7
  • 42
  • 53
  • can you please provide me the source code – Harshit Agarwal Aug 28 '14 at 13:30
  • You probably need to rework your entire task in order to parse the entire JSON inside doInBackground and *then* use the resulting data to update your UI, launch new Activities etc ... – 2Dee Aug 28 '14 at 13:50