我正在尝试在我的项目中实现一个线程示例,我希望将用户输入的用户名和密码传入线程,这样就可以检测到帐户是否存在,但我是难以正确实施它。任何帮助将不胜感激。
以下是我收到的错误之一。
类'checkPassword2'必须在'AsyncTask'中声明为abstract或实现抽象方法>'doInBackground(Params ...)
package com.example.liam.ca3;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.os.AsyncTask;
public class LoginScreen extends Activity {
private final static String TAG = "Sleep";
private static final String defName = "admin";
private static final String defPassword = "admin1";
private ProgressBar mProgressBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginscreen);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
final EditText uname = (EditText) findViewById(R.id.username_edittext);
final EditText passwd = (EditText) findViewById(R.id.password_edittext);
final Button loginButton = (Button) findViewById(R.id.login_button);
loginButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new checkPassword2().execute(uname, passwd);
if (checkPassword(uname.getText(), passwd.getText())) {
Log.i("8", "accepted password");
// Create an explicit Intent for starting the HelloAndroid Activity
Intent startApp = new Intent(LoginScreen.this,
MainActivity.class);
// Use the Intent to start the HelloAndroid Activity
startActivity(startApp);
} else {
Log.i("8", "failed password");
uname.setText("");
passwd.setText("");
}
}
});
}
private boolean checkPassword(Editable uname, Editable passwd) {
Log.i("2", uname.toString());
Log.i("2", passwd.toString());
if(uname.toString().equalsIgnoreCase(defName) && passwd.toString().equalsIgnoreCase(defPassword))
{
Log.i("8", "this should work");
return true;
}
Log.i("8", "this shouldn't work");
return false;
}
private class checkPassword2 extends AsyncTask<Integer, Integer, String> {
protected int doInBackground(String... values) {
int result = 0;
String nameTry = values[0];
String passTry = values[1];
if(nameTry.equalsIgnoreCase(defName) && passTry.equalsIgnoreCase(defPassword))
{
Log.i("8", "this should work");
result = 1;
}
else {
Log.i("8", "this shouldn't work");
result = 2;
}
for (int i = 1; i < 11; i++) {
sleep();
publishProgress(i * 10);
}
return result;
}
protected void onProgressUpdate(Integer... progress) {
mProgressBar.setProgress(progress[0]);
}
protected void onPostExecute(Long result) {
mProgressBar.setVisibility(ProgressBar.INVISIBLE);
}
private void sleep() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Log.e(TAG, e.toString());
}
}
}
}
答案 0 :(得分:3)
查看文档:{{3}}
扩展AsyncTask时,需要指定三个泛型:
在您的代码中,声明的泛型类型与参数类型之间存在不匹配。你的泛型是&#34;整数,整数,字符串&#34;:
现在让我们看看你的代码......
doInBackground(String... values)
Misatch:它需要Integer参数,指定String参数。但是你确实返回了一个整数。
onPostExecute(Long result)
不匹配:它需要Integer参数,指定Long参数。
确保方法中的类型与generics子句中指定的类型相符,一切都应该没问题。