这是我的onCreate方法问题是应用程序显示吐司“完成!”但它不执行li.execute(Email,Password);
并且它崩溃了。而已
我希望应用程序首先在li.execute(Email,Password);
内执行操作。
PS:loginBackgroundWorker是一个扩展AsyncTask<String,String,String>
的类,它有onPreExecute()
和doInBackground()
以及onPostExecute()
方法。
该应用程序不会显示任何错误或警告,但它会在模拟器中崩溃。
这是代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
li = new loginBackgroundWorker(LoginActivity.this);
//loginBackgroundWorker is a class that extends AsyncTask
EditTextEmail =(EditText) findViewById(R.id.emailField);
EditTextPassword =(EditText) findViewById(R.id.passwordField);
loginButton = (CardView) findViewById(R.id.loginButton);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Email = EditTextEmail.getText().toString().trim();
Password = EditTextPassword.getText().toString().trim();
li.execute(Email,Password);
Toast.makeText(LoginActivity.this,"Finish !",Toast.LENGTH_LONG).show();
}
});
}
这里是AsyncTask类(我不认为这些函数中的内容对你很重要)
public class loginBackgroundWorker extends AsyncTask<String,String,String> {
Context context;
public String email;
public String password;
public static final String login_url="http://192.168.0.104/login.php";
public loginBackgroundWorker(Context context) {
this.context = context;
}
@Override
public void onPreExecute() {
//Toast.makeText(context,"preExecute",Toast.LENGTH_LONG).show();
}
@Override
public String doInBackground(String... params) {
try {
email = params[0];
password = params[1];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&"
+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine())!= null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
public void onPostExecute(String result) {
Toast.makeText(context,"Onpost",Toast.LENGTH_LONG).show();
String r=result.trim();
Boolean aBoolean = true;
if (result.equals("1"))
{
Toast.makeText(context,"Bien Connecté",Toast.LENGTH_LONG).show();
aBoolean = false;
}
else if (result.equals(""))
r = "Login ou mot de passe incorrect";
if (aBoolean)
{
Toast.makeText(context,r,Toast.LENGTH_LONG).show();
}
}
}
这是StackTrace
--------- beginning of crash
05-26 14:56:24.023 14756-14756/com.example.zaariou.blacklist E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zaariou.blacklist, PID: 14756
java.lang.IllegalStateException: Cannot execute task: the task is already running.
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:593)
at android.os.AsyncTask.execute(AsyncTask.java:551)
at com.example.zaariou.blacklist.LoginActivity$1.onClick(LoginActivity.java:58)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
AsyncTask
的实例只能运行一次。
如果您想重复使用AsyncTask
,则必须初始化loginBackgroundWorker
的新实例并运行它。
将loginBackgroundWorker
初始化移至OnClickListener.onClick
句柄