App crashes giving index out of bounds in logcat while adding footer to listview

て烟熏妆下的殇ゞ 提交于 2019-12-19 11:16:11

问题


I have a listview which loads more listitems when it reaches 20rh listitem using onscroll listener

I want to add a footer saying loading more while more listitems are getting loaded

When i reach the 20th listitem the app crashes saying idex out of bounds in logct

my activity

public class InterActivity extends Activity 
{

SwipeMenuListView listview;
//ListView listview;

View mFooterView;






@Override
public void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.inter_layout);
    shrdPreference = new SharedPreference();


    //Execute RemoteDataTask AsyncTask

    new RemoteDataTask().execute();
}




private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();


        mProgressDialog = new ProgressDialog(InterActivity.this);
        mProgressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        // Set progressdialog title
        //mProgressDialog.setTitle("BEGINNER CODES");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading. Please wait loading ....");
        mProgressDialog.setIndeterminate(true);
        mProgressDialog.setCancelable(false);
        mProgressDialog.setIndeterminateDrawable(getResources().getDrawable(R.anim.progress_handler));
        // Show progressdialog
        mProgressDialog.show();

    }


    @Override
    protected Void doInBackground(Void... params) {
        // my do in backgroung process

    }

    @Override
    protected void onPostExecute(Void result) {
        // Locate the listview in listview_main.xml
    //  listview = (ListView) findViewById(R.id.inter_layoutListView);

        listview = (SwipeMenuListView) findViewById(R.id.inter_layoutListView);
        // Pass the results into ListViewAdapter.java
        adapter = new FinalAdapter(InterActivity.this,
                                      codelist);
         mFooterView = LayoutInflater.from(InterActivity.this).inflate(R.layout.footer_layout, null);                        

        // Binds the Adapter to the ListView
    listview.setAdapter(adapter);


        // Close the progressdialog
        mProgressDialog.dismiss();

// setup onscrll listener

        listview.setOnScrollListener(new OnScrollListener() {

                @Override
                public void onScrollStateChanged(AbsListView view,
                                                 int scrollState) { // TODO Auto-generated method stub
                    int threshold = 1;
                    int count = listview.getCount();

                    if (scrollState == SCROLL_STATE_IDLE) {
                        if (listview.getLastVisiblePosition() >= count
                            - threshold) {
                            // Execute LoadMoreDataTask AsyncTask
                            new Loadmore().execute();
                        }
                    }
                }

                @Override
                public void onScroll(AbsListView view, int firstVisibleItem,
                                     int visibleItemCount, int totalItemCount) {
                    // TODO Auto-generated method stub

                }

            });




    }
}





private class Loadmore extends AsyncTask<Void, Void, Void> {


    @Override
    protected void onPreExecute() {

        super.onPreExecute();
    listview.addFooterView(mFooterView);

        }
    @Override
    protected Void doInBackground(Void... params) {
        // my load mmore data process


    }

    @Override
    protected void onPostExecute(Void result) {
        int position = listview.getLastVisiblePosition();



    adapter.notifyDataSetChanged();
        listview.removeFooterView(mFooterView);


listview.setSelectionFromTop(position, 0);


    }
}

   }    

logcat

02-29 15:53:11.546 23342 23342 E AndroidRuntime
FATAL EXCEPTION: main 02-29 15:53:11.546 23342 23342 E
AndroidRuntime
java.lang.IndexOutOfBoundsException: Invalid index 9, size is 0 02-29 15:53:11.546 23342 23342 E AndroidRuntime
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at java.util.ArrayList.get(ArrayList.java:304)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at com.enlightenme.pac.FinalAdapter.getItem(FinalAdapter.java:55)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at com.enlightenme.pac.FinalAdapter.getView(FinalAdapter.java:83)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at com.baoyz.swipemenulistview.SwipeMenuAdapter.getView(SwipeMenuAdapter.java:65)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.widget.AbsListView.obtainView(AbsListView.java:2588)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.widget.ListView.makeAndAddView(ListView.java:1840)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.widget.ListView.fillDown(ListView.java:681)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.widget.ListView.fillSpecific(ListView.java:1339)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.widget.ListView.layoutChildren(ListView.java:1639)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.widget.AbsListView.onLayout(AbsListView.java:2427)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.view.View.layout(View.java:14938)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.view.ViewGroup.layout(ViewGroup.java:4601)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.view.View.layout(View.java:14938)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.view.ViewGroup.layout(ViewGroup.java:4601)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.view.View.layout(View.java:14938)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.view.ViewGroup.layout(ViewGroup.java:4601)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1694)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1552)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.widget.LinearLayout.onLayout(LinearLayout.java:1465) 02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.view.View.layout(View.java:14938)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.view.ViewGroup.layout(ViewGroup.java:4601)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.view.View.layout(View.java:14938) 02-29 15:53:11.546 23342 23342 E AndroidRuntime at android.view.ViewGroup.layout(ViewGroup.java:4601) 02-29 15:53:11.546 23342 23342 E AndroidRuntime at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2212)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2026)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1236)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5160)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791) 02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.view.Choreographer.doCallbacks(Choreographer.java:591)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.view.Choreographer.doFrame(Choreographer.java:561) 02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)

02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.os.Handler.handleCallback(Handler.java:725) 02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.os.Handler.dispatchMessage(Handler.java:92) 02-29 15:53:11.546 23342 23342 E AndroidRuntime
at android.os.Looper.loop(Looper.java:176) 02-29 15:53:11.546 23342 23342 E AndroidRuntime at android.app.ActivityThread.main(ActivityThread.java:5365) 02-29 15:53:11.546 23342 23342 E AndroidRuntime
at java.lang.reflect.Method.invokeNative(Native Method) 02-29 15:53:11.546 23342 23342 E AndroidRuntime
at java.lang.reflect.Method.invoke(Method.java:511) 02-29 15:53:11.546 23342 23342 E AndroidRuntime at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 02-29 15:53:11.546 23342 23342 E AndroidRuntime
at

new logcat

java.lang.ClassCastException: com.baoyz.swipemenulistview.SwipeMenuListView$1 cannot be cast to android.widget.HeaderViewListAdapter
02-29 17:54:03.976 27184 27184 E   AndroidRuntime                               at android.widget.ListView.removeFooterView(ListView.java:396)
02-29 17:54:03.976 27184 27184 E   AndroidRuntime                               at com.enlightenme.pac.InterActivity$Loadmore.onPostExecute(InterActivity.java:383)

回答1:


Create footer_view.xml and than just try it onCreate

View footerView =  ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
list.addFooterView(footerView);


来源:https://stackoverflow.com/questions/35697446/app-crashes-giving-index-out-of-bounds-in-logcat-while-adding-footer-to-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!