I am working on an android project and ran into problems. I have an activity which has a "Write a Review" button which opens a dialog, which in turn has two buttons "Done" and "No Thanks". The "Done" button executes an AsyncTask
which stores data to server but it causes the app to crash with a NullPointerException
error. Here's the code:
writeReviewBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
// Create custom dialog object
final Dialog dialog = new Dialog(GetReviewActivity.this);
// Include dialog.xml file
dialog.setContentView(R.layout.write_review_activity);
// Set dialog title
dialog.setTitle("Your review is valuable");
// set values for custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.tvWR);
//text.setText("Custom dialog Android example.");
ImageView image = (ImageView) dialog.findViewById(R.id.smiley);
dialog.show();
Button writeButton = (Button) dialog.findViewById(R.id.buttonWR);
writeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new WriteReviewAsyncTask().execute();
}
});
Button declineButton = (Button) dialog.findViewById(R.id.buttonNoThanks);
// if decline button is clicked, close the custom dialog
declineButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
}catch (Exception e){
e.printStackTrace();
}
}
});
The writeButton
is the "Done" button.
Here's the AsyncTask
implementation:
public class WriteReviewAsyncTask extends AsyncTask<Void,Void,Void>{
final EditText etWR = (EditText)findViewById(R.id.etWR);
String Review = etWR.getText().toString();
@Override
protected Void doInBackground(Void... params) {
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
try {
Log.i("HAPPENED: ","Working");
userLocalStore = new UserLocalStore(GetReviewActivity.this);
User user = userLocalStore.getLoggedInUser();
dataToSend.add(new BasicNameValuePair("reviewer_name",user.username));
dataToSend.add(new BasicNameValuePair("item_name",ItemName));
dataToSend.add(new BasicNameValuePair("review",Review));
HttpParams httpParams = getHttpRequestParams();
HttpClient httpClient = new DefaultHttpClient(httpParams);
HttpPost httpPost = new HttpPost(SERVER_ADDRESS+"StoreReview.php");
httpPost.setEntity(new UrlEncodedFormEntity(dataToSend));
httpClient.execute(httpPost);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
UpdateRecyclerView();
}
private HttpParams getHttpRequestParams() {
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams,
CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams,
CONNECTION_TIMEOUT);
return httpRequestParams;
}
}
And the LogCat error:
09-23 22:12:22.110 30716-30716/app.usrete.jayant.delvemitt E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at app.usrete.jayant.delvemitt.childscreen.getreviews.getreview.GetReviewActivity$WriteReviewAsyncTask.<init>(GetReviewActivity.java:330)
at app.usrete.jayant.delvemitt.childscreen.getreviews.getreview.GetReviewActivity$2$1.onClick(GetReviewActivity.java:133)
at android.view.View.performClick(View.java:4475)
at android.view.View$PerformClick.run(View.java:18786)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
Please help me out guys!