0

我要退出 frustradet,但我不明白这个错误的意义,我希望你们中的一些人能帮助我。我将创建一个通过几个 ListView 的菜单,它适用于第一面,但在菜单的第二面,它会在任何点击时崩溃。我尝试了一些方法,但是 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)
4

1 回答 1

0

你想做什么?

              if (name ==  "001"){

          "myList.clear();"
          This code clears data in mylist();

   "user2 = myList2.get(position);"     
   This code retrieves position of other list thats y error rises


              name2 = (String)user2.get("ueberschrift");
              mainListView.setAdapter(ab);
              mainListView.requestLayout();

              }
于 2013-04-03T18:41:33.290 回答