java Android应用程序在使用AsyncTask执行方法时崩溃
我的应用程序包含名为BackgroundWorker的类,该类扩展了AsyncTask,用于处理与在线服务器的通信。 我正在尝试从两个不同的类激活backgroundWorker类——一个来自MainActivity类,另一个来自Register类。 运行以下行时:
BackgroundWorker bgw = new BackgroundWorker(MainActivity.this);
bgw.execute(type , username , password);
从MainActivity类-一切都很好,但当试图从另一个类(即注册类)运行它们时,如下所示:
BackgroundWorker bgw = new BackgroundWorker(Register.this);
bgw.execute(type ,name, surname, age, username , password);
应用程序崩溃,出现以下弹出消息:
Unfortunately, APPNAME has stopped
我知道这是
bgw.execute()
这是因为我调试了程序
以下是我的两个类的完整代码:
MainActivity(其中execute方法非常有效):
public class MainActivity extends AppCompatActivity {
EditText etUser;
EditText etPass;
Button btnLogin;
Button btnRegister;
TextView tvStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUser = (EditText) findViewById(R.id.editText_username);
etPass = (EditText) findViewById(R.id.editText_password);
btnLogin = (Button) findViewById(R.id.button_login);
btnRegister = (Button) findViewById(R.id.button_reg);
onLoginClick();
onRegisterClick();
}
public void onLoginClick() {
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username = etUser.getText().toString();
String password = etPass.getText().toString();
String type = "login";
BackgroundWorker bgw = new BackgroundWorker(MainActivity.this);
bgw.execute(type , username , password);
}
});
}
public void onRegisterClick() {
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent openRegisterActivity = new Intent(MainActivity.this , Register.class);
startActivity(openRegisterActivity);
}
});
}
}
注册类(执行使应用程序崩溃):
public class Register extends AppCompatActivity {
Button btnSubmit;
EditText etName;
EditText etSurname;
EditText etAge;
EditText etUsername;
EditText etPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
btnSubmit = (Button) findViewById(R.id.button_submit);
etName = (EditText) findViewById(R.id.editText_name);
etSurname = (EditText) findViewById(R.id.editText_surname);
etAge = (EditText) findViewById(R.id.editText_age);
etUsername = (EditText) findViewById(R.id.editText_username);
etPassword = (EditText) findViewById(R.id.editText_password);
onSubmit();
}
public void onSubmit() {
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = etName.getText().toString();
String surname = etSurname.getText().toString();
String age = etAge.getText().toString();
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
String type = "Register";
BackgroundWorker bgw = new BackgroundWorker(Register.this);
bgw.execute(type ,name, surname, age, username , password);
}
});
}
}
我认为这与崩溃无关,但无论如何,这里是我的BackgroundWorker类(扩展了AsyncTask)代码:
public class BackgroundWorker extends AsyncTask <String , Integer , String> {
Context context;
AlertDialog alertDialog;
public BackgroundWorker(Context userContext) {
this.context = userContext;
}
@Override
protected String doInBackground(String... params) {
// STACK OVERFLOW SAY TO PUT THIS FOR DUBUG
if(安卓.os.Debug.isDebuggerConnected())
安卓.os.Debug.waitForDebugger();
// Reading the arguments in the order I sent them in MainActivity
String type = params[0];
// Making structure for updating during the wat using Toasts (and showing the first one
int progressValue = 0; // Before if
publishProgress(progressValue);
// Initial String that will hold the text to read from server
String result = "";
// Initial the URL String
String loginURL = "http://noche.netai.net/login.php";
String registerURL = "http://noche.netai.net/register.php";
if (type.equals("login")) {
String username = params[1];
String password = params[2];
progressValue++; //1
publishProgress(progressValue);
try {
URL url = new URL(loginURL);
progressValue++; //2
publishProgress(progressValue);
// Building the HttpUrlConnection object
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
progressValue++; //3
publishProgress(progressValue);
// Building the writing infrastructure
// IOException occurs after the commit of the next line
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
progressValue++; //4
publishProgress(progressValue);
// Building the message to the PHP script
String post_data = URLEncoder.encode("user_name" , "UTF-8") + "=" + URLEncoder.encode(username , "UTF-8")
+ "&" + URLEncoder.encode("user_pass" , "UTF-8") + "=" + URLEncoder.encode(password , "UTF-8");
progressValue++; //5
publishProgress(progressValue);
// Writing the data
bufferedWriter.write(post_data);
progressValue++; //6
publishProgress(progressValue);
// Closing all writing structures
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
// Building the Reading Infrastructure
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream , "iso-8859-1"));
// Reading the data
String line;
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
// Closing all reading structures
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
progressValue = 10;
publishProgress(progressValue);
} catch (IOException e) {
progressValue = 11;
publishProgress(progressValue);
e.printStackTrace();
}
}
if (type.equals("register")) {
String name = params[1];
String surname = params[2];
String age = params[3];
String username = params[4];
String password = params[5];
progressValue++; //1
publishProgress(progressValue);
try {
URL url = new URL(registerURL);
progressValue++; //2
publishProgress(progressValue);
// Building the HttpUrlConnection object
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
progressValue++; //3
publishProgress(progressValue);
// Building the writing infrastructure
// IOException occurs after the commit of the next line
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
progressValue++; //4
publishProgress(progressValue);
// Building the message to the PHP script
String post_data = URLEncoder.encode("name" , "UTF-8") + "=" + URLEncoder.encode(name , "UTF-8")
+ "&" + URLEncoder.encode("surname" , "UTF-8") + "=" + URLEncoder.encode(surname , "UTF-8")
+ "&" + URLEncoder.encode("age" , "UTF-8") + "=" + URLEncoder.encode(age , "UTF-8")
+ "&" + URLEncoder.encode("username" , "UTF-8") + "=" + URLEncoder.encode(username , "UTF-8")
+ "&" + URLEncoder.encode("password" , "UTF-8") + "=" + URLEncoder.encode(password , "UTF-8");
progressValue++; //5
publishProgress(progressValue);
// Writing the data
bufferedWriter.write(post_data);
progressValue++; //6
publishProgress(progressValue);
// Closing all writing structures
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
// Building the Reading Infrastructure
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream , "iso-8859-1"));
// Reading the data
String line;
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
// Closing all reading structures
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
progressValue = 10;
publishProgress(progressValue);
} catch (IOException e) {
progressValue = 11;
publishProgress(progressValue);
e.printStackTrace();
}
}
return result;
}
@Override
protected void onProgressUpdate(Integer... values) {
// FOR CHECKING THE PROBLEM WITH CONNECTING TO THE WAMP SERVER
/*
int progressValue = values[0];
// Before IF statement
if (progressValue == 0) Toast.makeText(this.context , "Before if" , Toast.LENGTH_SHORT).show();
// After IF statement
if (progressValue == 1) Toast.makeText(this.context , "After if" , Toast.LENGTH_SHORT).show();
// After URL statement
if (progressValue == 2) Toast.makeText(this.context , "After URL Setting" , Toast.LENGTH_SHORT).show();
// Before Writing data
if (progressValue == 3) Toast.makeText(this.context , "After making HTML" , Toast.LENGTH_SHORT).show();
// After Writing data
if (progressValue == 4) Toast.makeText(this.context , "After making writing infra" , Toast.LENGTH_SHORT).show();
// After Writing data
if (progressValue == 5) Toast.makeText(this.context , "Before Writing data" , Toast.LENGTH_SHORT).show();
// After Writing data
if (progressValue == 6) Toast.makeText(this.context , "After Writing data" , Toast.LENGTH_SHORT).show();
// After Writing data
if (progressValue == 10) Toast.makeText(this.context , "First exception" , Toast.LENGTH_SHORT).show();
// After Writing data
if (progressValue == 11) Toast.makeText(this.context , "IO Exception" , Toast.LENGTH_SHORT).show();
*/
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}
@Override
protected void onPostExecute(String result) {
String subRes = result.substring(0,26); // To cut all the bulshit
alertDialog.setMessage(subRes);
alertDialog.show();
}
}
编辑:这里是堆栈跟踪(我想,我是Android开发的新手,所以不太确定堆栈跟踪应该在哪里):
E/AndroidRuntime: FATAL EXCEPTION: main java.lang.StringIndexOutOfBoundsException: length=0; regionStart=0; regionLength=26
at java.lang.String.startEndAndLength(String.java:583)
at java.lang.String.substring(String.java:1464)
at com.example.nok_000.mysqlwith安卓project.BackgroundWorker.onPostExecute(BackgroundWorker.java:246)
at com.example.nok_000.mysqlwith安卓project.BackgroundWorker.onPostExecute(BackgroundWorker.java:29)
at 安卓.os.AsyncTask.finish(AsyncTask.java:631)
at 安卓.os.AsyncTask.access$600(AsyncTask.java:177)
at 安卓.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at 安卓.os.Handler.dispatchMessage(Handler.java:99)
at 安卓.os.Looper.loop(Looper.java:137)
at 安卓.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
谢谢——, 诺姆
共 (0) 个答案