在我的自定义DialogPreference
中,我使用执行onClick
的自定义View.onClickListener
覆盖肯定按钮的AsyncTask
事件。我的课看起来大致如下:
public class LoginDialog extends DialogPreference {
private Context mContext;
public LoginDialog(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
setDialogLayoutResource(R.layout.login_dialog);
}
@Override
protected void showDialog(Bundle bundle) {
super.showDialog(bundle);
Button pos = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
pos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
...
new LoginTask(mContext, LoginDialog.this).doInBackground(user_name, user_password);
}
});
}
public class LoginTask extends AsyncTask<String, Void, Boolean> {
private LoginDialog mDialog;
private Context mContext;
public LoginTask(Context cx, LoginDialog loginDialog) {
super();
mDialog = loginDialog;
mContext = cx;
}
protected void onPreExecute() {
// this is not called, at least nothing shows up in the logs
}
protected Boolean doInBackground(String... params) {
// this is called
}
protected void onPostExecute(Boolean success) {
// this is not called, at least nothing shows up in the logs
}
}
}
为什么没有调用onPre/PostExecute
方法?我试着显示祝酒词,记录东西,但没有出现。我确认调用了doInBackground
方法。
答案 0 :(得分:4)
new LoginTask(mContext, LoginDialog.this).doInBackground(user_name, user_password);
你不应该调用doInBackground。请改为调用execute()。
ASyncTask这是在线程规则
的第4行明显的文件和状态中答案 1 :(得分:3)
new LoginTask(mContext, LoginDialog.this).doInBackground(user_name, user_password);
这就是你的问题。
应使用AsyncTask
方法启动 execute()
。
您可以在http://developer.android.com/reference/android/os/AsyncTask.html
查找文档因此,在您的情况下,代码应如下所示:
new LoginTask(mContext, LoginDialog.this).execute(user_name, user_password);
答案 2 :(得分:0)
您需要通过调用AsyncTask
方法启动execute()
,而不是通过创建doInBackground
类的实例来调用LoginTask
:
new LoginTask(mContext, LoginDialog.this).execute(user_name, user_password);
答案 3 :(得分:0)
您应该调用AsyncTask.execute方法。永远不要自己调用doInBackground,它会自动调用。
答案 4 :(得分:0)
因为你只是调用AsyncTask的doInBackground
函数是错误的
像这样调用异步任务
new LoginTask(mContext, LoginDialog.this).execute(params);
这样,首先你的onPreExecute()将在DIB之前被调用,当DIB完成它的任务onPostExecute
时会被调用。