尝试访问Android中doInBackground()返回的对象时遇到了一些问题。当我的按钮onClick时,它将执行 GetEventDetailAsyncTask 类中的doInBackground():
viewDtlEventBtn.setOnClickListener(new OnClickListener(){
public void onClick(View v){
//eventModel.setEventID(String.valueOf(eventIDTV.getText()));
new GetEventDetailAsyncTask(new GetEventDetailAsyncTask.OnRoutineFinished() {
public void onFinish() {
// Passing whole object with value into another activity
Intent eventDtlIntent = new Intent(context, EventDetailMain.class);
eventDtlIntent.putExtra("eventObj", eventModel);
eventDtlIntent.putExtra("eventCommentObj", eventCommentModel);
context.startActivity(eventDtlIntent);
}
}).execute(String.valueOf(eventIDTV.getText()));
}
});
然后在我的 GetEventDetailAsyncTask 类中,它将执行返回EventComment对象的 EventController 类中的方法:
public class GetEventDetailAsyncTask extends AsyncTask<String, Integer, EventComment> {
EventController eventCtrl = new EventController();
Context context;
EventComment comment = new EventComment();
public interface OnRoutineFinished { // interface
void onFinish();
}
private OnRoutineFinished mCallbacks;
public GetEventDetailAsyncTask(OnRoutineFinished callback) { // constructor with
// interface
mCallbacks = callback;
}
public GetEventDetailAsyncTask() {
} // empty constructor to maintain compatibility
public GetEventDetailAsyncTask(Context context){
this.context = context;
}
@Override
protected Double doInBackground(String... params) {
try {
//eventCtrl.getEventDetailByID(params[0]);
eventCtrl.getEventCommentByID(params[0]);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(EventComment result) {
comment = result;
if (mCallbacks != null)
mCallbacks.onFinish(); // call interface on finish
}
protected void onProgressUpdate(Integer... progress) {
}
}
EventController类
public EventComment getEventCommentByID(String eventID) throws JSONException {
EventComment eventCommentModel = new EventComment();
String page;
JSONArray jsonArray;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(ENeighbourhoodActivity.URL
+ "/EventServlet?action=GetCommentById&eventID=" + eventID
+ "");
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
page = "{\'EventComment\':" + responseString + "}";
try {
JSONObject jsonObject = new JSONObject(page);
jsonArray = jsonObject.getJSONArray("EventComment");
int length = jsonArray.length();
for (int i = 0; i < length; i++) {
JSONObject attribute = jsonArray.getJSONObject(i);
String commentBy = attribute.getString("commentBy");
String commentDate = attribute.getString("commentDate");
String commentTime = attribute.getString("commentTime");
String commentDesc = attribute.getString("commentDesc");
eventCommentModel.setCommentBy(commentBy);
eventCommentModel.setCommentDate(commentDate);
eventCommentModel.setCommentTime(commentTime);
eventCommentModel.setCommentDesc(commentDesc);
}
} catch (JSONException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return eventCommentModel;
}
然而,当我的按钮是onClick并将eventComment对象作为参数传递给 EventChat 类时,它为null并且在尝试访问对象本身时我得到NullPointerException。
有什么想法吗?提前谢谢。
答案 0 :(得分:0)
这段代码有些可疑:
try {
eventCtrl.getEventDetailByID(params[0]);
eventCtrl.getEventCommentByID(params[0]);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
仔细看看,你打电话给eventCtrl.getEventDetailByID(params[0]);
,但不要保存它返回的内容。
您需要创建EventComment
的实例并使用它:
myEvenComment = eventCtrl.getEventCommentByID(params[0]);