国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

android - Error: java.lang.IndexOutOfBoundsException: Invalid index 2

瀏覽:82日期:2024-02-27 13:25:31

問題描述

How to remove the seperator line in footerLayout? I have a footerLayout below the listView, used to display the totalAmount as shown below. If I click the seperator line in footerLayout, my app crashed.

android - Error: java.lang.IndexOutOfBoundsException: Invalid index 2

My MainActivity

AllAdapter obj = new AllAdapter(getApplication(), search, listview,imageView,text,button);footerLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.under_listview, null);totalAmount = (TextView) footerLayout.findViewById(R.id.amount);

LogCat error

java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) at java.util.ArrayList.get(ArrayList.java:304) at com.example.tony.monthlyexpenses.adapter.AllAdapter.getItem(AllAdapter.java:61) at com.example.tony.monthlyexpenses.QuickExpenses$1.onItemClick(QuickExpenses.java:88) at android.widget.AdapterView.performItemClick(AdapterView.java:301)

The error pointed to listView onClickListener

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> listView, View view, final int position, long id) {mClickedPosition = position;Expenses o = (Expenses) obj.getItem(position);String day = o.getDate(); }});

AllAdapter

public Expenses getItem(int position) {return search.get(position); }

The footerLayout is supposed to be displayed outside the listView, not inside. How can I get rid of this ?

I also have activity_main.xml, AllAdapter class, all_adapter.xml for ListView and also under_listview.xml for the footerLayout.

activity_main

AllAdapter

under_listview

How to move the footerLayout out from the ListView ?

I add android:footerpidersEnabled='false' now become like this

android - Error: java.lang.IndexOutOfBoundsException: Invalid index 2

But still clickable !!!

誰知道問題出在哪?

android - Error: java.lang.IndexOutOfBoundsException: Invalid index 2

footerLayout被按時如何不出現灰色?

android - Error: java.lang.IndexOutOfBoundsException: Invalid index 2

問題解答

回答1:

很簡單,但也很容易出錯的問題,加了footer后,你的listview item數量是3,但adapter的viewcount其實并沒有變成3,所以在你點擊footer時執行的是obj.getItem(2),肯定是數組越界異常了。對于添加了header或footer的listview,正確的取item方法應該是

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> listView, View view, final int position, long id) {Expenses o = (Expenses) listView.getAdapter().getItem(position);if(o != null){ mClickedPosition = position; //Expenses o = (Expenses) obj.getItem(position); String day = o.getDate();} }});

header或footer屬于AdapterView的子view,listView.getAdapter().getItem(position);能確保你取2的position時不越界,再做對象空判斷。

回答2:

你不能使用setOnItemClickListener 來作為footview的點擊事件,我認為你應該單獨的去設置例如 footview.setonClickListener(new OnClickListener{}); 祝你好運

回答3:

你這個是數組下標越界了啊,你的數組size是2,所以對應的下標只能是0和1,但是你在使用的時候用了2,錯誤顯示你有個無效的index 2,你自己找下第61行和第88行,看是否有地方調用了index是2的

回答4:

將footerLayout移出listView的寫法是

listview.addFooterView(footerLayout, null, false);

標簽: java
相關文章:
主站蜘蛛池模板: 武川县| 厦门市| 桐城市| 潞西市| 堆龙德庆县| 临颍县| 砚山县| 行唐县| 澄江县| 江川县| 浮梁县| 河间市| 昂仁县| 抚远县| 旬阳县| 泾川县| 永泰县| 乌兰浩特市| 洪洞县| 宣城市| 高唐县| 古浪县| 日喀则市| 拜泉县| 辽宁省| 绥中县| 仲巴县| 新沂市| 武威市| 黑山县| 丁青县| 海城市| 枣阳市| 汉阴县| 探索| 读书| 临漳县| 藁城市| 丰宁| 阿坝| 万州区|