【问题标题】:Force Close on listview onitemclick在列表视图 onitemclick 上强制关闭
【发布时间】:2013-07-08 03:38:44
【问题描述】:

我的onitemclick 看起来像这样:

//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);
    }
});

我的 BeerTastes 看起来像:

public class BeerTastes {

    String taste;
    double percent;

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

    }

}

当我单击列表视图中的某个项目时,我收到此强制关闭错误:

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

【问题讨论】:

  • TastePage.java:31 中有什么?似乎它会抛出 ArrayIndexOutOfBoundsException。
  • @sandrstar 那是问题谢谢!
  • 了解如何阅读堆栈跟踪是一项宝贵的技能,而且并不难掌握。请参阅this question 以获取一些快速指示。
  • BeerTastes o =arg0.getAdapter().getItem(arrg2);在适配器中返回 public BeerTastes getItem(int position){ return list.get(position);}

标签: android android-listview android-activity


【解决方案1】:

而不是使用

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

使用您用于设置列表视图的 arraylist

例如,如果您的 BeerTastes 类型的 arrayList 是 myArrayList。然后在 onItemClickListener 中使用这一行

BeerTastes o = myArrayList.get(arg2);

这一行将给出在列表视图中单击的项目。

【讨论】:

    【解决方案2】:

    它的说法ArrayIndexOutOfBoundsException

    我认为问题出在这一行

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

    【讨论】:

      【解决方案3】:

      我认为您正在使用从基本适配器扩展的自定义适配器。请注意此类,因为列表大小获取和设置方法多次出现此问题。

      在调试模式下检查您的自定义适配器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多