Mike
Mike

Reputation: 6839

Force Close on listview onitemclick

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

Upvotes: 0

Views: 398

Answers (3)

Tugrul
Tugrul

Reputation: 1808

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.

Upvotes: 0

newBie
newBie

Reputation: 118

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.

Upvotes: 1

Chinmoy Debnath
Chinmoy Debnath

Reputation: 2824

Its saying ArrayIndexOutOfBoundsException

i think problem is in this line

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

Upvotes: 0

Related Questions