0

My onitemclick looks like this:

//set up clicks
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView <? > arg0, View arg1,
        int arg2, long arg3) {
        BeerTastes o = (BeerTastes) arg0.getItemAtPosition(arg2);

        String tempTaste = o.taste;

        Intent myIntent = new Intent(c, TastePage.class);
        myIntent.putExtra("taste", tempTaste);
        c.startActivity(myIntent);
    }
});

My BeerTastes looks like:

public class BeerTastes {

    String taste;
    double percent;

    public BeerTastes(String t, double p){
        taste = t;
        percent = p;

    }

}

When I click an item in my listview I get this force close error:

07-07 23:28:31.253    2009-2009/com.beerportfolio.beerportfoliopro E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.beerportfolio.beerportfoliopro/com.example.beerportfoliopro.TastePage}: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
        at android.app.ActivityThread.access$600(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:155)
        at android.app.ActivityThread.main(ActivityThread.java:5536)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
        at com.example.beerportfoliopro.TastePage.onCreate(TastePage.java:31)
        at android.app.Activity.performCreate(Activity.java:5066)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
        ... 11 more
Renjith
  • 5,783
  • 9
  • 31
  • 42
Mike
  • 6,751
  • 23
  • 75
  • 132
  • 1
    What is at TastePage.java:31 ? Seems it throws ArrayIndexOutOfBoundsException. – sandrstar Jul 08 '13 at 03:39
  • @sandrstar that was the problem thanks! – Mike Jul 08 '13 at 04:01
  • Understanding how to read stack traces is a valuable skill and one that isn't that hard to pick up. See [this question](http://stackoverflow.com/questions/12688068/how-to-read-and-understand-the-java-stack-trace) for some quick pointers. – John Moffitt Jul 08 '13 at 04:07
  • BeerTastes o =arg0.getAdapter().getItem(arrg2); Return in adapter as public BeerTastes getItem(int position){ return list.get(position);} – Ramesh Akula Jul 08 '13 at 04:21

3 Answers3

1

Instead of using

BeerTastes o=(BeerTastes)arg0.getItemAtPosition(arg2);

Use the arraylist you are using for setting listview.

For eg if your arrayList of type BeerTastes is myArrayList. Then in onItemClickListener use this line

BeerTastes o = myArrayList.get(arg2);

This line will give the item clicked in listview.

Royston Pinto
  • 6,681
  • 2
  • 28
  • 46
newBie
  • 118
  • 11
0

Its saying ArrayIndexOutOfBoundsException

i think problem is in this line

BeerTastes o=(BeerTastes)arg0.getItemAtPosition(arg2);
Chinmoy Debnath
  • 2,814
  • 16
  • 20
0

I think that you are using custom adapter which is extends from Base adapter. Be carefull about this class because so many times list size getting and setting methods occurs this problem.

Check your custom adapter in debug mode.

Tugrul
  • 1,760
  • 4
  • 24
  • 39