I have Asynctask in my activity. The problem is when I setText in onPostExecute() it gave me NullPointerException. the textview in a custom dialog XML. I tried to put the TextView variable as a global but it's the same thing. The toast is working fine. This is my code:
private class LikeDislike extends AsyncTask<String, Integer, JSONObject> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected JSONObject doInBackground(String... params) {
UserFunctions userFunction = new UserFunctions();
JSONObject json;
json = userFunction.likeDislike(params[0]);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
super.onPostExecute(json);
try {
if (json.getString(KEY_SUCCESS) != null) {
//registerErrorMsg.setText("");
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
String like = json.getString("like");
String disLike = json.getString("dislike");
TextView likeText = (TextView) findViewById(R.id.likenumber);
TextView dislikeText = (TextView) findViewById(R.id.dislikenumber);
likeText.setText(like);
dislikeText.setText(disLike);
//TODO
}else{
Context context = getApplicationContext();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "error in parsing", duration);
toast.show();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
You are saying the TextViews are in your Dialog then you have to call myDialog.findViewById() to get the views. Otherwise findViewById() try to find it in your Activity.