私は frustradet をやめようとしていますが、このエラーの要点がわかりません。あなたの何人かが私を助けてくれることを願っています. いくつかの ListView を通過するメニューを作成します。これは最初の面で機能しますが、メニューの 2 番目の面では、クリックされた時点でクラッシュします。いくつかの方法を試しましたが、OnItemClickListener が機能しないか、IndexOutOfBoundException エラーが発生します。インデックス クラッシュを回避するために、try/catch ブラケットを取り付けました
エラーが簡単に解決できることを願っています:)そしてあなたの助けに感謝します. エラーが「位置」の50行目にあることは知っていますが、その解決策は見つかりませんでした。
package com.sample.menuv2;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
public class Menu extends Activity {
String name, name2, name3;
HashMap<String, Object> user, user2, user3;
SimpleAdapter aa, ab, ac;
ArrayList<HashMap<String, Object>> myList, myList2, myList3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView mainListView = (ListView) findViewById(R.id.main_listview);
// Create Hash Map
myList = new ArrayList<HashMap<String, Object>>();
myList2 = new ArrayList<HashMap<String, Object>>();
myList3 = new ArrayList<HashMap<String, Object>>();
// Fill data
addDataToList1();
addDataToList2();
addDataToList3();
//Adapter
aa = new SimpleAdapter(this, myList, R.layout.row, new String[] {"ueberschrift", "sub", "icon"}, new int[] {R.id.txt_ueberschrift, R.id.txt_sub, R.id.img_left});
ab = new SimpleAdapter(this, myList2, R.layout.row, new String[] {"ueberschrift", "sub", "icon"}, new int[] {R.id.txt_ueberschrift, R.id.txt_sub, R.id.img_left});
ac = new SimpleAdapter(this, myList3, R.layout.row, new String[] {"ueberschrift", "sub", "icon"}, new int[] {R.id.txt_ueberschrift, R.id.txt_sub, R.id.img_left});
mainListView.setAdapter(aa);
mainListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
user = myList.get(position);
name = (String)user.get("ueberschrift");
if (name == "001"){
myList.clear();
user2 = myList2.get(position);
name2 = (String)user2.get("ueberschrift");
mainListView.setAdapter(ab);
mainListView.requestLayout();
}
if (name2 == "020"){
myList2.clear();
user3 = myList3.get(position);
name3 = (String)user3.get("ueberschrift");
mainListView.setAdapter(ac);
mainListView.requestLayout();
}
if (name3 == "100"){
myList2.clear();
mainListView.setAdapter(ab);
mainListView.requestLayout(); }
}
});
}
public void addDataToList3() {
HashMap<String, Object> map1 = new HashMap<String, Object>();
map1.put("icon", R.drawable.user_image1);
map1.put("ueberschrift", "100");
map1.put("sub", "Ebene 3");
HashMap<String, Object> map2 = new HashMap<String, Object>();
map2.put("icon", R.drawable.user_image2);
map2.put("ueberschrift", "200");
map2.put("sub", "Ebene 3");
HashMap<String, Object> map3 = new HashMap<String, Object>();
map3.put("icon", R.drawable.user_image3);
map3.put("ueberschrift", "300");
map3.put("sub", "Ebene 3");
HashMap<String, Object> map4 = new HashMap<String, Object>();
map4.put("icon", R.drawable.user_image4);
map4.put("ueberschrift", "400");
map4.put("sub", "Ebene 3");
HashMap<String, Object> map5 = new HashMap<String, Object>();
map5.put("icon", R.drawable.user_image5);
map5.put("ueberschrift", "500");
map5.put("sub", "Ebene 3");
HashMap<String, Object> map6 = new HashMap<String, Object>();
map6.put("icon", R.drawable.user_image5);
map6.put("ueberschrift", "600");
map6.put("sub", "Ebene 3");
myList3.add(map1);
myList3.add(map2);
myList3.add(map3);
myList3.add(map4);
myList3.add(map5);
myList3.add(map6);
}
public void addDataToList2() {
HashMap<String, Object> map01 = new HashMap<String, Object>();
map01.put("icon", R.drawable.user_image1);
map01.put("ueberschrift", "010");
map01.put("sub", "Ebene 2");
HashMap<String, Object> map02 = new HashMap<String, Object>();
map02.put("icon", R.drawable.user_image2);
map02.put("ueberschrift", "020");
map02.put("sub", "Ebene 2");
HashMap<String, Object> map03 = new HashMap<String, Object>();
map03.put("icon", R.drawable.user_image3);
map03.put("ueberschrift", "030");
map03.put("sub", "Ebene 2");
HashMap<String, Object> map04 = new HashMap<String, Object>();
map04.put("icon", R.drawable.user_image4);
map04.put("ueberschrift", "040");
map04.put("sub", "Ebene 2");
HashMap<String, Object> map05 = new HashMap<String, Object>();
map05.put("icon", R.drawable.user_image5);
map05.put("ueberschrift", "050");
map05.put("sub", "Ebene 2");
HashMap<String, Object> map06 = new HashMap<String, Object>();
map06.put("icon", R.drawable.user_image5);
map06.put("ueberschrift", "060");
map06.put("sub", "Ebene 2");
HashMap<String, Object> map07 = new HashMap<String, Object>();
map07.put("icon", R.drawable.user_image3);
map07.put("ueberschrift", "070");
map07.put("sub", "Ebene 2'");
myList2.add(map01);
myList2.add(map02);
myList2.add(map03);
myList2.add(map04);
myList2.add(map05);
myList2.add(map06);
myList2.add(map07);
}
public void addDataToList1(){
HashMap<String, Object> map1 = new HashMap<String, Object>();
map1.put("icon", R.drawable.user_image1);
map1.put("ueberschrift", "001");
map1.put("sub", "Ebene 1");
HashMap<String, Object> map2 = new HashMap<String, Object>();
map2.put("icon", R.drawable.user_image2);
map2.put("ueberschrift", "002");
map2.put("sub", "Ebene 1");
HashMap<String, Object> map3 = new HashMap<String, Object>();
map3.put("icon", R.drawable.user_image3);
map3.put("ueberschrift", "003");
map3.put("sub", "Ebene 1");
HashMap<String, Object> map4 = new HashMap<String, Object>();
map4.put("icon", R.drawable.user_image4);
map4.put("ueberschrift", "004");
map4.put("sub", "Ebene 1");
HashMap<String, Object> map5 = new HashMap<String, Object>();
map5.put("icon", R.drawable.user_image5);
map5.put("ueberschrift", "005");
map5.put("sub", "Ebene 1");
HashMap<String, Object> map6 = new HashMap<String, Object>();
map6.put("icon", R.drawable.user_image5);
map6.put("ueberschrift", "006");
map6.put("sub", "Ebene 1");
myList.add(map1);
myList.add(map2);
myList.add(map3);
myList.add(map4);
myList.add(map5);
myList.add(map6);
}
}
および LogCat 画面:
04-02 23:54:36.681: E/AndroidRuntime(720): FATAL EXCEPTION: main
04-02 23:54:36.681: E/AndroidRuntime(720): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
04-02 23:54:36.681: E/AndroidRuntime(720): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
04-02 23:54:36.681: E/AndroidRuntime(720): at java.util.ArrayList.get(ArrayList.java:304)
04-02 23:54:36.681: E/AndroidRuntime(720): at com.sample.menuv2.Menu$1.onItemClick(Menu.java:50)
04-02 23:54:36.681: E/AndroidRuntime(720): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
04-02 23:54:36.681: E/AndroidRuntime(720): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
04-02 23:54:36.681: E/AndroidRuntime(720): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
04-02 23:54:36.681: E/AndroidRuntime(720): at android.widget.AbsListView$1.run(AbsListView.java:3168)
04-02 23:54:36.681: E/AndroidRuntime(720): at android.os.Handler.handleCallback(Handler.java:605)
04-02 23:54:36.681: E/AndroidRuntime(720): at android.os.Handler.dispatchMessage(Handler.java:92)
04-02 23:54:36.681: E/AndroidRuntime(720): at android.os.Looper.loop(Looper.java:137)
04-02 23:54:36.681: E/AndroidRuntime(720): at android.app.ActivityThread.main(ActivityThread.java:4340)
04-02 23:54:36.681: E/AndroidRuntime(720): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 23:54:36.681: E/AndroidRuntime(720): at java.lang.reflect.Method.invoke(Method.java:511)
04-02 23:54:36.681: E/AndroidRuntime(720): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-02 23:54:36.681: E/AndroidRuntime(720): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-02 23:54:36.681: E/AndroidRuntime(720): at dalvik.system.NativeStart.main(Native Method)