我是android开发的新手。在 mainactivity 中使用 JSON 时我没有问题,但将代码移动到 ListFragment 时出现问题
这一行什么也没发生: JSONObject json = userFunctions.announcementFeed(myID, myType);
这是完整的代码:
package com.example.demo123;
import java.util.ArrayList;
import java.util.HashMap;
import library.UserFunctions;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;
public class AnnouncementList extends ListFragment {
private static final String TAG = "MyAnnouncement";
JSONArray announcementList = null;
UserFunctions userFunctions;
String myID = MainActivity.myID;
String myType = MainActivity.myType;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.i(TAG,"START ANNOUNCEMENT LIST");
ArrayList<HashMap<String, String>> theAnnnouncement = new ArrayList<HashMap<String, String>>();
JSONObject json = userFunctions.announcementFeed(myID, myType);
try {
if (json.getString("success") != null) {
announcementList = json.getJSONArray("announceDT");
for (int i = 0; i < announcementList.length(); i++) {
JSONObject c = announcementList.getJSONObject(i);
String title = c.getString("topic");
String course = c.getString("course") + " - " + c.getString("courseTitle");
String created = "By " + c.getString("entered_by") + " | " + c.getString("entry_date");
HashMap<String, String> map = new HashMap<String, String>();
map.put("title", title);
map.put("course", course);
map.put("created", created);
theAnnnouncement.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
String[] from = { "title","course","created" };
int[] to = { R.id.title,R.id.course,R.id.created};
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), theAnnnouncement, R.layout.list_item, from, to);
setListAdapter(adapter);
}
}