我想將輸入的密碼值傳遞給一個對話框,並將它傳遞給asyntask,以便它可以發送到數據庫進行比較。我在密碼字段處得到nullpointerexception。看起來價值沒有通過。我該如何解決這個問題?將字符串傳遞給Asyntask
對話框,需要密碼才能繼續:
public void onClick(View view) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View promptView = layoutInflater.inflate(
R.layout.prompt_password, null);
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setView(promptView);
// Set an EditText view to get user input
final EditText input = (EditText) promptView
.findViewById(R.id.passwordInput);
alert.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String pass = (input.getText()).toString();
// Do something with value!
Log.d("Value", pass);
ProgressDialog progressDialog = new ProgressDialog(DisplayReqItemInfo.this);
progressDialog.setMessage("Cheking password...");
ItemEdit itemEdit = new ItemEdit(DisplayReqItemInfo.this, progressDialog);
itemEdit.execute();
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Canceled.
dialog.cancel();
}
});
AlertDialog dialog = alert.create();
dialog.show();
}
});
}
這是假設檢索值類:
public class ItemEdit extends AsyncTask<String, Void, Integer> {
private ProgressDialog progressDialog;
private DisplayReqItemInfo activity;
private int responseCode = 0;
public ItemEdit(DisplayReqItemInfo activity, ProgressDialog progressDialog)
{
this.activity = activity;
this.progressDialog = progressDialog;
}
@Override
protected void onPreExecute()
{
progressDialog.show();
}
protected Integer doInBackground(String... arg0) {
TextView PID = (TextView)activity.findViewById(R.id.req_pid);
EditText passwordEdit = (EditText)activity.findViewById(R.id.passwordInput);
String pid = PID.getText().toString();
String password = passwordEdit.getText().toString();
ItemFunction itemFunction = new ItemFunction();
JSONObject json = itemFunction.requestItem(pid, password);
// check for response
try {
if (json.getString(KEY_SUCCESS) != null) {
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
responseCode = 1;
}else{
responseCode = 0;
}
}
} catch (NullPointerException e) {
e.printStackTrace();
}
catch (JSONException e) {
e.printStackTrace();
}
return responseCode;
}
@Override
protected void onPostExecute(Integer responseCode)
{
if (responseCode == 1) {
progressDialog.dismiss();
Intent i = new Intent();
i.setClass(activity.getApplicationContext(), MainMenu.class);
activity.startActivity(i);
}
else {
progressDialog.dismiss();
}
}
}
'itemEdit.execute( 「yourString」);'和在'doInBackground'上使用'arg0 [0]' –