Reputation: 237
I am trying to pass the password value entered to a dialog box and pass it to the asyntask so that it could be sent to the database to compare. I am getting nullpointerexception at the password field. It seems like the value wasn't passed. How do I fix this problem?
Dialog box which require password to continue:
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();
}
});
}
The class which is suppose to retrieve the value:
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();
}
}
}
Upvotes: 0
Views: 81
Reputation: 1392
There can be no screen I/O from inside the AsyncTask.doInBackground function. You can do the screen I/O in the onPreExecute or onPostExecute functions, which have access to the UI thread. So you can get the value from the EditText field in onPreExecute and put it into a variable that is global to AsyncTask's methods and read by doInBackground.
(Using your example)
public class ItemEdit extends AsyncTask<String, Void, Integer> {
String password;
String pid;
@Override
protected void onPreExecute()
{
TextView PID = (TextView)activity.findViewById(R.id.req_pid);
EditText passwordEdit = (EditText)activity.findViewById(R.id.passwordInput);
pid = PID.getText().toString();
password = passwordEdit.getText().toString();
}
protected Integer doInBackground(String... arg0) {
ItemFunction itemFunction = new ItemFunction();
JSONObject json = itemFunction.requestItem(pid, password);
. . .
}
}
You can also pass parameters into the AsyncTask initializer and pass via the class global:
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();
// start background task
mAuthTask = new UserLoginTask(pid, password);
mAuthTask.execute();
public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
private String userLoginId;
private String userPassword;
private UserLoginTask(String loginId, String password) {
userLoginId = loginId;
userPassword = password;
}
@Override
protected Boolean doInBackground(Void... params) {
. . .
// Do something with userLoginId, userPassword
. . .
}
}
Upvotes: 0
Reputation: 4001
You can pass parameter in your execute
ItemEdit itemEdit = new ItemEdit(DisplayReqItemInfo.this, progressDialog);
itemEdit.execute(new String[] { "yourstring" });
In your Async
there is doInBackground
protected Integer doInBackground(String... arg0) {
String response = args0[0];
TextView PID = (TextView)activity.findViewById(R.id.req_pid);
Upvotes: 1