我想将输入的密码值传递给一个对话框,并将它传递给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]' –