我正在尝试使用用户名和密码登录。如果用户名是正确的,则转到另一个Activity
,否则保留在当前Activity
。
如果我第一次登录它成功,但第二次出错了。
错误.....
04-21 07:41:13.915: E/AndroidRuntime(2125): java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
04-21 07:41:13.915: E/AndroidRuntime(2125): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:578)
04-21 07:41:13.915: E/AndroidRuntime(2125): at android.os.AsyncTask.execute(AsyncTask.java:534)
04-21 07:41:13.915: E/AndroidRuntime(2125): at com.bis.storyanimationapp.LoginActivity$1.onClick(LoginActivity.java:35)
代码:
LoginActivity
//enter code here
package com.bis.storyanimationapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import com.bis.storyanimationapp.asynchtask.AsynchTask1;
import com.bis.storyanimationapp.view.LoginView;
import com.example.storyanimationapp.R;
public class LoginActivity extends Activity {
LoginView loginView;
AsynchTask1 objLoginAsycTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
loginView=new LoginView(this);
super.onCreate(savedInstanceState);
setContentView(loginView.getLayoutmain());
objLoginAsycTask=new AsynchTask1(LoginActivity.this,loginView);
loginView.getBtnLogin().setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
objLoginAsycTask.execute("");
//objLoginAsycTask.cancel(true);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
}
AsynchTask1
package com.bis.storyanimationapp.asynchtask;
import android.R.integer;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.Toast;
import com.bis.storyanimationapp.LoginActivity;
import com.bis.storyanimationapp.MainActivity;
import com.bis.storyanimationapp.view.LoginView;
public class AsynchTask1 extends AsyncTask<String,integer, String> {
Activity activity;
Context context;
private ProgressDialog signUpProcess;
LoginView view;
// private AsyncInterface asyInter;
public AsynchTask1(Activity activity,LoginView view){
this.activity=activity;
this.view=view;
context=activity.getApplicationContext();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
signUpProcess = ProgressDialog.show(this.activity, "Please Wait !!!",
"Submitting Data For Registration!!!", true, false);
Toast.makeText(context, "inpree ex.",10).show();
}
@Override
protected void onPostExecute(String result)
{
// TODO Auto-generated method stub
super.onPostExecute(result);
signUpProcess.dismiss();
Toast.makeText(context,"post",5).show();
myMethod(result);
}
private void myMethod(String result) {
// TODO Auto-generated method stub
if(view.getEdUserName().getText().toString().equals("abc")){
Intent i=new Intent(context,MainActivity.class);
activity.startActivity(i);
//this.activity.finish();
}else{
Toast.makeText(context,"invalid user name",5).show();
// Intent i=new Intent(context,LoginActivity.class);
// activity.startActivity(i);
//
}
}
@Override
protected String doInBackground(String... params) {
return view.getEdUserName().getText().toString();
//return null;
}
}
答案 0 :(得分:0)
正如@Raghunandan建议的那样,每次都创建一个asyntask的新实例:
public void onClick(View v) {
new AsynchTask1().execute("");
}
});
答案 1 :(得分:0)
你试试这个代码:
loginView.getBtnLogin().setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
objLoginAsycTask=new AsynchTask1(LoginActivity.this,loginView);
objLoginAsycTask.execute((Void) null);
}
});
}
答案 2 :(得分:0)
AsyncTask
个实例只能使用一次。
相反,只需将您的任务称为new AsynchTask1().execute("");
来自AsyncTask API文档
答案 3 :(得分:0)
每次登录时尝试创建AsyncTask
的新实例。
@Override
public void onClick(View v) {
AsyncTask1 at = new AsyncTask1()
at.execute()
}