我对这段代码有疑问:
for(int i=0; i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
String name = json_data.getString("post_title");
String created_date = json_data.getString("created_date");
String outPut = name + "-" + created_date;
postList.add(createPost("blog_posts", outPut));
Log.e("log_tag", "Name: "+name);
}
我收到了这个错误。
09-26 22:55:18.642: E/AndroidRuntime(754): FATAL EXCEPTION: main
09-26 22:55:18.642: E/AndroidRuntime(754): java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
09-26 22:55:18.642: E/AndroidRuntime(754): at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:160)
09-26 22:55:18.642: E/AndroidRuntime(754): at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:126)
09-26 22:55:18.642: E/AndroidRuntime(754): at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
09-26 22:55:18.642: E/AndroidRuntime(754): at android.widget.AbsListView.obtainView(AbsListView.java:2033)
09-26 22:55:18.642: E/AndroidRuntime(754): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
09-26 22:55:18.642: E/AndroidRuntime(754): at android.widget.ListView.onMeasure(ListView.java:1155)
09-26 22:55:18.642: E/AndroidRuntime(754): at android.view.View.measure(View.java:12723)
09-26 22:55:18.642: E/AndroidRuntime(754): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
09-26 22:55:18.642: E/AndroidRuntime(754): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
09-26 22:55:18.642: E/AndroidRuntime(754): at android.view.View.measure(View.java:12723)
09-26 22:55:18.642: E/AndroidRuntime(754): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
09-26 22:55:18.642: E/AndroidRuntime(754): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
09-26 22:55:18.642: E/AndroidRuntime(754): at android.view.View.measure(View.java:12723)
09-26 22:55:18.642: E/AndroidRuntime(754): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
这个问题可能有重复,但我尝试搜索答案,但没有一个有帮助。任何帮助,将不胜感激。
当我删除这段代码时,这工作正常。
SimpleAdapter simpleAdapter = new SimpleAdapter(
MainActivity.this, postList, R.layout.postlist,
new String[] {"posts"},
new int[] {R.id.post_title,R.id.posted_date});
listView.setAdapter(simpleAdapter);
private HashMap<String, String> createPost(
String name, String created_date) {
HashMap<String, String> blogitem = new HashMap<String, String>();
blogitem.put(name, created_date);
return blogitem;
}