我的活动中有 Asynctask。问题是当我在 onPostExecute() 中设置文本时,它给了我 NullPointerException。自定义对话框 XML 中的文本视图。我试图将 TextView 变量作为全局变量,但它是一回事。吐司工作正常。这是我的代码:
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();
}
}
}