我尝试从片段中的JSON数据创建ListView,但是我有各种各样的麻烦才能正确显示:
字段
public class Endpoints {
//constants for field references
public static final String PUBLICURL = "publicURL";
public static final String REGION = "region";
public static final String TYPE = "type";
public static final String NAME = "name";
//fields
String publicURL;
String region;
String type;
String name;
//getters and setters
public String getPublicURL() {
return publicURL;
}
public String getRegion() {
return region;
}
public String getType() {
return type;
}
public String getName() {
return name;
}
public void setPublicURL(String publicURL) {
this.publicURL = publicURL;
}
public void setRegion(String region) {
this.region = region;
}
public void setType(String type) {
this.type = type;
}
public void setName(String name) {
this.name = name;
}
PARSER
public class EndpointsParser {
public static List<Endpoints> parseFeed(String content) {
try {
JSONArray ar = new JSONArray(content);
List<Endpoints> endpointsList = new ArrayList<>();
for (int i = 0; i < ar.length(); i++) {
JSONObject obj = ar.getJSONObject(i);
Endpoints endpoint = new Endpoints();
endpoint.setName(obj.getString("name"));
endpoint.setType(obj.getString("type"));
endpoint.setRegion(obj.getString("region"));
endpoint.setPublicURL(obj.getString("publicURL"));
endpointsList.add(endpoint);
}
return endpointsList;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
ADAPTER
public class EndpointsAdapter extends ArrayAdapter<Endpoints> {
private Context context;
private List<Endpoints> endpointsList;
public EndpointsAdapter(Context context, int resource, List<Endpoints> objects){
super(context, resource, objects);
this.context = context;
this.endpointsList = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.endpoint_list, parent, false);
return view;
}
FRAGMENT OnCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String endpoints = "{\"access\": {\"token\": {\"issued_at\": \"2014-11-07T01:32:21.151767\", \"expires\": \"2014-11-07T02:32:21Z\", \"id\": \"1a7bdd8ce9604dd199c86f905092c59e\", \"tenant\": {\"description\": \"Admin\", \"enabled\": true, \"id\": ...]}}}";
View rootView = inflater.inflate(R.layout.fragment_overview, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.overviewTV);
ListView listView = (ListView) rootView.findViewById(R.id.overviewLV);
//For display testing
//textView.setText(endpoints);
List<Endpoints> endpointsList;
endpointsList = EndpointsParser.parseFeed(endpoints);
EndpointsAdapter adapter = new EndpointsAdapter(getActivity(), R.layout.endpoint_list, endpointsList);
listView.setAdapter(adapter);
return rootView;
}
我无法让它工作,而且错误来自的例外情况并不清楚:
11-07 04:48:54.751 2627-2627/com.stackerz.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.stackerz.app, PID: 2627
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stackerz.app/com.stackerz.app.Stackerz}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
at android.widget.ListView.setAdapter(ListView.java:480)
at android.app.ListFragment.setListAdapter(ListFragment.java:240)
at android.app.ListFragment.ensureList(ListFragment.java:424)
at android.app.ListFragment.onViewCreated(ListFragment.java:203)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.Activity.performStart(Activity.java:5240)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
您需要在适配器中使用@Override getCount()
方法。
@Override
public int getCount() {
return endpointsList.size();
}