我目前正在嘗試實現一個線程示例到我的項目中,我想將用戶輸入的用戶名和密碼傳入線程,以便在帳戶存在時檢測到它或不,但我在正確實施它時遇到困難。任何幫助將不勝感激。Android:傳遞字符串到asyncTask
這是我收到的錯誤之一。
類 'checkPassword2' 必須聲明爲抽象或實現抽象方法>'doInBackground(參數...)在 '的AsyncTask'
包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());
}
}
}
}
你的參數順序錯誤。 'doInBackground()'獲得類定義中的第一個參數。另外,'onPostExecute()'應該除了一個'Integer'而不是'Long'以及你擁有它的方式。 – codeMagic 2014-12-18 22:18:12