我是android编程的新手,请各位朋友帮我解决这个异常 我发给你我的登录代码和logcat文件。 当我点击确定登录按钮时,我正试图登录我的应用程序,它显示了一个例外。
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
这里我的登录代码是
public class Login extends Activity
{
}
答案 0 :(得分: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();
}
}
你的AsyncTask实现就像这样
像这样调用任务
LoginTask localLoginTask = new LoginTask();
String[] arr = new String[] {localEditText1.getText().toString(),localEditText2.getText().toString()};
localLoginTask.execute(arr);
你有方法
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);
}
}
单击按钮后应调用此方法
答案 1 :(得分:0)
您正在尝试访问doInBackground
内的用户界面,这不是很好。
尝试点击按钮
编写以下行 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.
您可以在doInBackGround
内访问这些值
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.
}