开发者

android - How to handle android runtime error

开发者 https://www.devze.com 2023-03-17 22:04 出处:网络
When my application is running i am getting the android runtime error java.lang.IndexOutOfBoundsException. How to handle it?

When my application is running i am getting the android runtime error java.lang.IndexOutOfBoundsException. How to handle it?

The following is my stack trace:

07-05 17:34:31.906: ERROR/AndroidRuntime(657): Uncaught handler: thread main exiting due to uncaught exception
07-05 17:34:31.937: ERROR/AndroidRuntime(657): java.lang.IndexOutOfBoundsException
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at java.util.LinkedList.get(LinkedList.java:453)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at com.ibkr.roadbrake.RB_UpcomingExits$UpcomingResultsListViewAdapter2.getView(RB_UpcomingExits.java:2232)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at com.ibkr.roadbrake.RB_UpcomingExits$8.onClick(RB_UpcomingExits.java:3921)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at android.view.View.performClick(View.java:2364)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at android.view.View.onTouchEvent(View.java:4179)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at android.view.View.dispatchTouchEv开发者_运维百科ent(View.java:3709)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:852)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at android.os.Looper.loop(Looper.java:123)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at android.app.ActivityThread.main(ActivityThread.java:4363)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at java.lang.reflect.Method.invokeNative(Native Method)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at java.lang.reflect.Method.invoke(Method.java:521)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-05 17:34:31.937: ERROR/AndroidRuntime(657):     at dalvik.system.NativeStart.main(Native Method)


so... in getCount method of your adapter .... do this

public int getCount(){

  int size = 1;
  if(arralyst !=null){

    size = arraylist.size();
  }  
  return size;
}


It means you are trying to access an item in an array over it's limit. That is to say you are trying to access the 6th element in a 5-element list.

Hope that makes sense, unless you post some code I cannot help you further...


The IndexOutOfBoundsException means that you are trying to access an element in an array (or List) which does not exist. Here an example:

    int[] array = {0,1,2};
    Log.v("Name", array[5]);

This example code tries to access the sixth element of the array, but only three elements exist.

Search your class and function in the StackTrace which cause the error and then check why you get the exception.


If you have a header or footer view in the list - don't forget to adjust the click position when getting an item in your UpcomingResultsListViewAdapter2.


Besides the obvious -- explicitly accessing an index which doesn't exist -- this error commonly occurs when you are iterating over a List and within your loop, you remove an element:

for (Foo foo : list) {
    if (isBar(foo)) {
        list.remove(foo);
    }
}

The iterator doesn't know about the changes, and it can throw an exception.

0

精彩评论

暂无评论...
验证码 换一张
取 消