0

I am new in android programming please friends help me out to solve this exception I am sending you my login code and logcat file. I am trying to login in my app when I click ok login button it shows me an exception.

08-05 09:50:23.050: E/AndroidRuntime(1001): FATAL EXCEPTION: AsyncTask #1
08-05 09:50:23.050: E/AndroidRuntime(1001): Process: com.example.parking, PID: 1001
08-05 09:50:23.050: E/AndroidRuntime(1001): java.lang.RuntimeException: An error occured while executing doInBackground()
08-05 09:50:23.050: E/AndroidRuntime(1001):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
08-05 09:50:23.050: E/AndroidRuntime(1001):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
08-05 09:50:23.050: E/AndroidRuntime(1001):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
08-05 09:50:23.050: E/AndroidRuntime(1001):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
08-05 09:50:23.050: E/AndroidRuntime(1001):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-05 09:50:23.050: E/AndroidRuntime(1001):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-05 09:50:23.050: E/AndroidRuntime(1001):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-05 09:50:23.050: E/AndroidRuntime(1001):     at java.lang.Thread.run(Thread.java:841)
08-05 09:50:23.050: E/AndroidRuntime(1001): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
08-05 09:50:23.050: E/AndroidRuntime(1001):     at com.example.parking.Login$LoginTask.doInBackground(Login.java:233)
08-05 09:50:23.050: E/AndroidRuntime(1001):     at com.example.parking.Login$LoginTask.doInBackground(Login.java:1)
08-05 09:50:23.050: E/AndroidRuntime(1001):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-05 09:50:23.050: E/AndroidRuntime(1001):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-05 09:50:23.050: E/AndroidRuntime(1001):     ... 4 more
08-05 09:50:23.240: I/Choreographer(1001): Skipped 54 frames!  The application may be doing too much work on its main thread.
08-05 09:50:23.490: I/Choreographer(1001): Skipped 32 frames!  The application may be doing too much work on its main thread.
08-05 09:50:23.780: I/Choreographer(1001): Skipped 72 frames!  The application may be doing too much work on its main thread.

08-05 09:50:27.650: I/Process(1001): Sending signal. PID: 1001 SIG: 9

here my Login code is

public class Login extends Activity
{

}
Roman C
  • 49,761
  • 33
  • 66
  • 176
Harshit
  • 153
  • 1
  • 11

2 Answers2

0
    class LoginTask extends AsyncTask<String, String, String> {
    LoginTask() {
    }

    protected String doInBackground(String[] paramArrayOfString) {
        //EditText localEditText1 = (EditText) Login.this
                //.findViewById(2131296260);
        //EditText localEditText2 = (EditText) Login.this
                //.findViewById(2131296259);
        //String str1 = localEditText1.getText().toString();
        //String str2 = localEditText2.getText().toString();

        str1=paramArrayOfString[0];
        str2=paramArrayOfString[1];
        if ((str1.length() < 1) || (str2.length() < 1))
            Login.login_message = "Error. Blank username or password!";
        while (true) {
            // return null;
            String str3 = "email=" + str1 + "&passd=" + str2;
            ConnectionManager.ConnectionResponse localConnectionResponse = new ConnectionManager()
                    .DoPost("", str3);
            if (localConnectionResponse.Length <= 0)
                break;
            Log.d("INFO:", "Length was greater than 0\n");
            Log.d("DATA:", localConnectionResponse.Data);
            String str4 = StringParser
                    .GetJSONString(localConnectionResponse.Data);
            int i = str4.indexOf("\"STATUS\"");
            if (i < 0) {
                if (str4.indexOf("error") < 0) {
                    Login.login_message = "Server Returned an Error.";
                    continue;
                }
                String str12 = StringParser.FetchFieldValue(StringParser
                        .SplitFields(str4.replace('}', ' ').replace('{',
                                ' ')), "error");
                if (str12 != null) {
                    Login.login_message = "Error: " + str12.trim();
                    continue;
                }
                Login.login_message = "Server returned an Error.";
                continue;
            }
            String str5 = str4.substring(i).replace('}', ' ')
                    .replace('{', ' ').trim();
            Log.d("FINAL_STR", str5);
            Map localMap = StringParser.SplitFields(str5);
            String str6 = StringParser.FetchFieldValue(localMap, "STATUS");
            String str7 = StringParser.FetchFieldValue(localMap, "user_id");
            if (str6 == null) {
                Login.login_message = "Server Response Error!!";
                continue;
            }
            Log.d("STATUS:", str6);
            if (str7 != null)
                Log.d("UID:", str7);
            int j = Integer.parseInt(str6);
            String str8 = null;
            switch (j) {
            case 1:
            default:
            case 0:
            case 2:
            case 3:
            }
            while (true) {
                if (str8 == null) {
                    // break label422;
                    Login.login_success = 1;
                }
                Login.login_message = str8;
                break;
                // str8 = "Invalid Password or E-Mail!!";
                // continue;
                // str8 = "User has been deactivated!!";
                // continue;
                // str8 = "This account is yet to be activated.";
            }
            // label422: Login.login_success = 1;
            String str9 = StringParser.FetchFieldValue(localMap, "role");
            String str10 = StringParser.FetchFieldValue(localMap, "area");
            String str11 = StringParser.FetchFieldValue(localMap, "street");
            if ((str9 == null) || (str10 == null) || (str11 == null)) {
                Login.login_success = 0;
                Login.login_message = "Server failed to return all credentials";
                continue;
            }
            int k = -1;
            if (str9.equalsIgnoreCase("INSPECTOR"))
                k = 0;
            while (true) {
                if (k >= 0) {
                    // break label558;
                    Login.user_region = "area=" + str10 + "&street="
                            + str11;
                    Login.this.user_id = str7;
                    Login.this.user_class = k;
                    Login.user_role = str9;
                }
                Login.login_message = "Unknown role was returned.";
                // break;
                if (str9.equalsIgnoreCase("CLAMPER")) {
                    k = 1;
                    continue;
                }
                if (str9.equalsIgnoreCase("DECLAMPER")) {
                    k = 2;
                    continue;
                }
                if (!str9.equalsIgnoreCase("TOWER"))
                    continue;
                k = 3;
            }
            /*
             * label558: Login.user_region = "area=" + str10 + "&street=" +
             * str11; Login.this.user_id = str7; Login.this.user_class = k;
             * Login.user_role = str9;
             */
        }
        while (true) {
            // break;
            Login.login_message = "Connection Failed! Check Settings.";
        }
        // return null;
    }

    protected void onPostExecute(String paramString) {
        Login.process_dlg.dismiss();
        if (Login.login_success == 0)
            Toast.makeText(Login.this, Login.login_message, 1).show();
        while (true) {
            // return;
            Login.this.switch_dashboard();
        }
    }

    protected void onPreExecute() {
        Login.login_message = null;
        Login.login_success = 0;
        Login.process_dlg = new ProgressDialog(Login.this);
        Login.process_dlg.setTitle("Connecting ...");
        Login.process_dlg.setMessage("Please wait");
        Login.process_dlg.setIndeterminate(true);
        Login.process_dlg.setCancelable(false);
        Login.process_dlg.show();
    }
}

Your your AsyncTask Implementation like this

Call the task like this

LoginTask localLoginTask = new LoginTask();
    String[] arr = new String[]   {localEditText1.getText().toString(),localEditText2.getText().toString()};

    localLoginTask.execute(arr);

You have the method

public void onLoginClicked(View paramView)
  {
 if (service_running != 0)
 {
 stopService();
 session_service = null;
  service_running = 0;
}
 login_success = 0;

  public void onLoginClicked(View paramView)
  {
    if (service_running != 0)
   { 
   stopService();
   session_service = null;
   service_running = 0;
   }
  login_success = 0;
  LoginTask localLoginTask = new LoginTask();
    String[] arr = new String[]   {  localEditText1.getText().toString(),
   localEditText2.getText().toString()};

    localLoginTask.execute(arr);
    }
  }

This method should be called after click on a button

Rajnish Mishra
  • 826
  • 5
  • 21
  • Its giving an error here localEditText1 and localEditText2 String[] arr = new String[] {localEditText1.getText().toString(),localEditText2.getText().toString()}; – Harshit Aug 05 '14 at 12:48
  • Oviously you need to update localEditText1=(EditText)findViewById(R.id.your_edit_text_id); – Rajnish Mishra Aug 05 '14 at 12:50
  • It feels like you are trying to use code generated from some decompiler ... I would suggest making a fresh layout.xml and using it Becasue your class includes findViewById(2131296260) here the id was not given and it was predefined it only happens after getting code from decompiler – Rajnish Mishra Aug 05 '14 at 12:51
  • ya you are saying right actually my sir provided this one code and say to me work on it. This code is not developed by me thats why I am getting alot of problem in this. – Harshit Aug 05 '14 at 13:05
  • The best I will suggest it making the layout yourself and writing your code yourself in that case you have any problem then any we can understand it and help you Thanks – Rajnish Mishra Aug 05 '14 at 13:14
  • hey can you provide me your mail id so that i will send you my code file to you.. and Thanks – Harshit Aug 05 '14 at 13:21
  • rajnish@radicallabs.com or rajnishmishra20@gmail.com – Rajnish Mishra Aug 05 '14 at 13:28
  • hey please check your mail I have sent you both files. Thanks – Harshit Aug 05 '14 at 13:57
  • hey I have edited my logcat and login code above please check it now whats wrong in that code please help me out. Thanks – Harshit Aug 05 '14 at 14:24
  • upadate your logcat after making these changes in doinbackground String str1=paramArrayOfString[0]; String str2=paramArrayOfString[1]; – Rajnish Mishra Aug 05 '14 at 14:28
  • i am sending you new question link with login code and log file please check it below http://stackoverflow.com/questions/25154094/fatal-exception-asynctask – Harshit Aug 06 '14 at 07:01
0

You are trying to access the UI inside doInBackground, which is not good.

Try writing the below lines on click of button

 EditText localEditText1 = (EditText)Login.this.findViewById(2131296260);
 EditText localEditText2 = (EditText)Login.this.findViewById(2131296259);
 String str1 = localEditText1.getText().toString();
 String str2 = localEditText2.getText().toString();
 if ((str1.length() < 1) || (str2.length() < 1))
 // use Toast message to notify the user that either of EditTexts is blank.
 else
 // here call your AsyncTask, like below
 new LoginTask().execute (str1, str2);  // str1 and str2 are username and password.

You can access these values inside doInBackGround like

 protected String doInBackground(String[] paramArrayOfString)
 {

     String strUserName = paramArrayOfString[0];
     String strPassword = paramArrayOfString[1];


     // Change  String str3 = "email=" + str1 + "&passd=" + str2;

      to 


      String str3 = "email=" + strUserName + "&passd=" + strPassword;

     // your remaining code.
 }
Aniruddha
  • 4,477
  • 2
  • 21
  • 39
  • hey will you please provide me complete code for doInBackground() – Harshit Aug 05 '14 at 12:59
  • For the I need to understand your code, your code is very long. I will try when I get time. – Aniruddha Aug 06 '14 at 03:53
  • I have modified, you should accept my answer if it solved your current problem. I cannot help if you don't accept the answer which helped to solve the current problem. If there's anything else then post it as different question and give the link here. You should appreciate by up voting and accepting the answer which helped you. – Aniruddha Aug 06 '14 at 04:00
  • hey its work lil but not properly i will put different question and will send u the link Thanks. – Harshit Aug 06 '14 at 06:42
  • I am sending you new question link check it below – Harshit Aug 06 '14 at 06:59