我是Android的新手并且在水平滑动中陷入困境。让我详细解释一下,如果用户点击ListView
项目,我在ListView
中有几个数据 - 详细说明页面打开,这里我需要水平滑动“详细说明”页面。我使用pageIndicator库下载并测试了一些教程。终于能够得到ListView
数据但是当点击列表项时它会强制关闭,logcat中的错误说OutOfBound Exception
请帮助我,谢谢。
Logcat异常
01-22 12:28:46.530: E/AndroidRuntime(1084): FATAL EXCEPTION: main
01-22 12:28:46.530: E/AndroidRuntime(1084): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
01-22 12:28:46.530: E/AndroidRuntime(1084): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
01-22 12:28:46.530: E/AndroidRuntime(1084): at java.util.ArrayList.get(ArrayList.java:311)
01-22 12:28:46.530: E/AndroidRuntime(1084): at com.jokes.LayoutTwo.onCreateView(LayoutTwo.java:70)
01-22 12:28:46.530: E/AndroidRuntime(1084): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
01-22 12:28:46.530: E/AndroidRuntime(1084): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
LayoutTwo.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.samples, null);
TextView txttitle = (TextView) root.findViewById(R.id.titles);
///////// The exception arises in below line ///////
txttitle.setText(Common.rowItems.get(Common.itemnumber).get("strtitle"));
TextView txtDescription = (TextView) root.findViewById(R.id.description);
txtDescription.setText(Common.rowItems.get(Common.itemnumber).get("strDesc"));
Intent in = getActivity().getIntent();
String strtitle = in.getStringExtra("strtitle");
String strDesc = in.getStringExtra("strDesc");
return root;
}
但是我已经在MainActivity中正确地给出了数组长度。 MainActivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem(titles[i], descriptions[i]);
rowItems.add(item);
}
listView = (ListView) findViewById(R.id.list);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
TextView txttl = (TextView) view.findViewById(R.id.title);
String strtitle = txttl.getText().toString();
TextView txtls = (TextView) view.findViewById(R.id.desc);
String strDesc = txtls.getText().toString();
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), ViewPagerStyle1Activity.class);
// sending data to new activity
i.putExtra("strtitle", strtitle);
i.putExtra("strDesc", strDesc);
startActivity(i);
}
}
答案 0 :(得分:1)
txttitle.setText(Common.rowItems.get(Common.itemnumber).get("strtitle"));
我想Common.rowItems
在调用此行时尚未填充。所以它是空的。现在Common.itemnumber
似乎是“0”。它要求一个空结构的第一个元素。
要解决此问题,您必须先检查Common.itemnumber
是否具有合理的值才能首先索引到Common.rowItems
。如果没有,那么设置默认文本或类似的东西。或者当然,确保已填写Common.rowItems
。但你仍然应该验证索引是否超出范围。
答案 1 :(得分:0)
像这样使用:
Intent in = getActivity().getIntent();
String strtitle = in.getStringExtra("strtitle");
String strDesc = in.getStringExtra("strDesc");
TextView txttitle = (TextView) root.findViewById(R.id.titles);
TextView txtDescription = (TextView) root.findViewById(R.id.description);
txttitle.setText(strtitle);
txtDescription.setText(strDesc);