. lang。IndexOutOfBoundsException:无效索引9,大小为1

时间:2022-10-22 20:38:56

I am working in android Pull to refresh listview.Its working fine ,but while refreshing listview ,I am trying to scroll down the list.i am getting this errors:

我正在使用android Pull刷新listview。它运行良好,但在刷新listview时,我试图向下滚动列表。我犯了这样的错误:

here is my code:

这是我的代码:

public void Callpagedetails() {

        try
            {


         URL = http://history_thread.php?token_id=" + Token + "&p=" +                       page_count;                   
                Log.d("TAG", "API URL IS " + URL);

                HttpClient mHttpClient = new DefaultHttpClient();

                HttpGet mGetMethod = new HttpGet(URL);

                HttpResponse mReponseMessage = mHttpClient.execute(mGetMethod);

                String Response = EntityUtils.toString(mReponseMessage.getEntity());

                Log.d("TAG", "O/P Response is " + Response);

                JSONArray responseObject = new JSONArray(Response);

                          for(int i=0; i < responseObject.length(); i++)

                    {
                        obj = responseObject.getJSONObject(i);

                        listhashmap = new HashMap<String, Object>();

                        listhashmap.put("text",obj.getString("text"));

                here i am storing the item in arraylist arraylistitems


                    }
}
catch(Exception e)
{
System.out.println(e);
}

    }

Where the arraylistitems is ArrayList<HashMap<String, Object>> arraylistitems;

其中arraylistitem为ArrayList > arraylistitem;

where Callpagedetails() is the method from where i am getting the data

Callpagedetails()是从哪里获取数据的方法

    01-20 18:22:17.755: E/InputEventReceiver(23393): Exception dispatching input event.
01-20 18:22:17.755: E/MessageQueue-JNI(23393): Exception in MessageQueue callback: handleReceiveCallback
01-20 18:22:17.775: E/MessageQueue-JNI(23393): java.lang.IndexOutOfBoundsException: Invalid index 9, size is 1
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at java.util.ArrayList.get(ArrayList.java:304)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.widget.AbsListView.onTouchEvent(AbsListView.java:3816)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at com.listview.HistoryListView.onTouchEvent(HistoryListView.java:316)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at com.listview.HistoryListView.onInterceptTouchEvent(HistoryListView.java:380)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1827)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1886)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1886)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1886)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1886)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1886)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2113)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1466)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.app.Activity.dispatchTouchEvent(Activity.java:2436)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2061)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.view.View.dispatchPointerEvent(View.java:7425)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3376)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3308)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4402)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4380)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4484)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.os.MessageQueue.nativePollOnce(Native Method)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.os.MessageQueue.next(MessageQueue.java:125)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.os.Looper.loop(Looper.java:124)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at android.app.ActivityThread.main(ActivityThread.java:4918)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at java.lang.reflect.Method.invokeNative(Native Method)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at java.lang.reflect.Method.invoke(Method.java:511)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
01-20 18:22:17.775: E/MessageQueue-JNI(23393):  at dalvik.system.NativeStart.main(Native Method)
01-20 18:22:17.785: E/AndroidRuntime(23393): FATAL EXCEPTION: main
01-20 18:22:17.785: E/AndroidRuntime(23393): java.lang.IndexOutOfBoundsException: Invalid index 9, size is 1
01-20 18:22:17.785: E/AndroidRuntime(23393):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at java.util.ArrayList.get(ArrayList.java:304)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.widget.AbsListView.onTouchEvent(AbsListView.java:3816)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at com.listview.HistoryListView.onTouchEvent(HistoryListView.java:316)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at com.listview.HistoryListView.onInterceptTouchEvent(HistoryListView.java:380)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1827)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1886)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1886)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1886)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1886)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1886)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2113)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1466)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.app.Activity.dispatchTouchEvent(Activity.java:2436)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2061)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.view.View.dispatchPointerEvent(View.java:7425)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3376)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3308)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4402)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4380)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4484)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.os.MessageQueue.nativePollOnce(Native Method)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.os.MessageQueue.next(MessageQueue.java:125)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.os.Looper.loop(Looper.java:124)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at android.app.ActivityThread.main(ActivityThread.java:4918)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at java.lang.reflect.Method.invokeNative(Native Method)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at java.lang.reflect.Method.invoke(Method.java:511)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
01-20 18:22:17.785: E/AndroidRuntime(23393):    at dalvik.system.NativeStart.main(Native Method)

Could somebody help me out. Thanks.

谁能帮我一下吗?谢谢。

6 个解决方案

#1


3  

i am not trying but sure it may be help you.

我不是在尝试,但肯定会对你有所帮助。

                JSONArray responseObject = new JSONArray(Response);
                listhashmap = new HashMap<String, Object>();
                for(int i=0; i < responseObject.length(); i++)
                {
                    obj = responseObject.getJSONObject(i);
                    listhashmap.put("text",obj.getString("text"));

                }

#2


0  

You should not touch an adapter's data in a non-UI thread.

不应该在非ui线程中触摸适配器的数据。

Your doInBackground() seems to set a count that's possibly used by an adapter to zero and also guessing that Callpagedetails() directly modifies the data used by the adapter.

doInBackground()似乎将适配器可能使用的计数设置为0,并猜测Callpagedetails()将直接修改适配器使用的数据。

Instead, modify the adapter's data in the UI thread. onPostExecute() is a good place for that.

相反,在UI线程中修改适配器的数据。onPostExecute()是一个很好的地方。

#3


0  

On your Callpagedetails, take a look at your for loop after instanced responseObject.

在Callpagedetails中,查看实例responseObject之后的for循环。

You keep initial listhashmap. It is why you listhashmap size is 1. Replace the new HashMap to outside for loop.

你保持初始listhashmap。这就是为什么listhashmap的大小是1。将新的HashMap替换为for循环。

for (int i = 0; i < responseObject.length(); i++) {
    obj = r esponseObject.getJSONObject(i);
    listhashmap = new HashMap < String, Object > ();  // problems code
    listhashmap.put("text", obj.getString("text"));
}

#4


0  

protected class RefreshTask extends AsyncTask>> {

受保护的类RefreshTask扩展AsyncTask>> {

        @Override
        protected ArrayList<HashMap<String, Object>> doInBackground(Void... params) {
            // TODO Auto-generated method stub
            page_count = 0;
            ArrayList<HashMap<String, Object>> data=Callpagedetails();
            return data;

        }

        protected void onPostExecute(ArrayList<HashMap<String, Object>> result) {
            super.onPostExecute(result);
        if(result!=null)
        {
             adapterfirst = new Questionadapter(historylist.this,result);
             listviewfirst = (RefeshListView) findViewById(R.id.listquestion);
             listviewfirst.setAdapter(adapterfirst);
                adapterfirst.notifyDataSetChanged();
        }
         yourlistView.postDelayed(new Runnable() {

            @Override
            public void run() {
                 new RefreshTask().execute();  
            }
        }, 1000);

}
        }
    }

Hi Priya ! try this procedure! It is always a good way to pass the objects to onpostExecute as a parameter! Hope this will fix your issue!

嗨Priya !试试这个程序!它始终是将对象传递到onpostExecute作为参数的好方法!希望这能解决你的问题!

#5


0  

This may not be exact . but try to modify accordingly,

这可能并不准确。但是试着相应地修改,

 public void Callpagedetails() {

    try
        {
     URL = http://history_thread.php?token_id=" + Token + "&p=" +                       page_count;                   
            Log.d("TAG", "API URL IS " + URL);

            HttpClient mHttpClient = new DefaultHttpClient();

            HttpGet mGetMethod = new HttpGet(URL);

            HttpResponse mReponseMessage = mHttpClient.execute(mGetMethod);

            String Response = EntityUtils.toString(mReponseMessage.getEntity());

            Log.d("TAG", "O/P Response is " + Response);

            JSONArray responseObject = new JSONArray(Response);

                      for(int i=0; i < responseObject.length(); i++)

                {
                    obj = responseObject.getJSONObject(i);


                }
  }
catch(Exception e)
{
 System.out.println(e);
}

}

and , in your

,在你的

doInBackground() {
   ....
 CallPageDetails();

}

and,

而且,

 onPostExecute() {
                    if(yourListView!=null) {
                  listhashmap = new HashMap<String, Object>();

                    listhashmap.put("text",obj.getString("text"));

            here YOU WILL BE storing the item in arraylist arraylistitems
  &&  also set, 
            yourAdapter.notifyDataSetChanged();
                 }
        .....
        .....


}

*-> Try at your Own Risk.

-冒你自己的风险。

#6


0  

arraylist hold object refrence so your list hold only last value becuase you are new object on same memory locaion.

arraylist保存对象的折射,所以你的列表只保存最后一个值,因为你是同一个内存上的新对象。

for (int i = 0; i < responseObject.length(); i++) {
    obj = r esponseObject.getJSONObject(i);
    listhashmap = new HashMap < String, Object > ();  // problems code
    listhashmap.put("text", obj.getString("text"));
}

replace with

替换为

for (int i = 0; i < responseObject.length(); i++) {
    obj = r esponseObject.getJSONObject(i);
 **HashMap<String, Object> listhashmap = new HashMap<String, Object>();**

                        listhashmap.put("text",obj.getString("text"));

                here i am storing the item in arraylist arraylistitems
                         rraylistitem.put(listhashmap);
}

#1


3  

i am not trying but sure it may be help you.

我不是在尝试,但肯定会对你有所帮助。

                JSONArray responseObject = new JSONArray(Response);
                listhashmap = new HashMap<String, Object>();
                for(int i=0; i < responseObject.length(); i++)
                {
                    obj = responseObject.getJSONObject(i);
                    listhashmap.put("text",obj.getString("text"));

                }

#2


0  

You should not touch an adapter's data in a non-UI thread.

不应该在非ui线程中触摸适配器的数据。

Your doInBackground() seems to set a count that's possibly used by an adapter to zero and also guessing that Callpagedetails() directly modifies the data used by the adapter.

doInBackground()似乎将适配器可能使用的计数设置为0,并猜测Callpagedetails()将直接修改适配器使用的数据。

Instead, modify the adapter's data in the UI thread. onPostExecute() is a good place for that.

相反,在UI线程中修改适配器的数据。onPostExecute()是一个很好的地方。

#3


0  

On your Callpagedetails, take a look at your for loop after instanced responseObject.

在Callpagedetails中,查看实例responseObject之后的for循环。

You keep initial listhashmap. It is why you listhashmap size is 1. Replace the new HashMap to outside for loop.

你保持初始listhashmap。这就是为什么listhashmap的大小是1。将新的HashMap替换为for循环。

for (int i = 0; i < responseObject.length(); i++) {
    obj = r esponseObject.getJSONObject(i);
    listhashmap = new HashMap < String, Object > ();  // problems code
    listhashmap.put("text", obj.getString("text"));
}

#4


0  

protected class RefreshTask extends AsyncTask>> {

受保护的类RefreshTask扩展AsyncTask>> {

        @Override
        protected ArrayList<HashMap<String, Object>> doInBackground(Void... params) {
            // TODO Auto-generated method stub
            page_count = 0;
            ArrayList<HashMap<String, Object>> data=Callpagedetails();
            return data;

        }

        protected void onPostExecute(ArrayList<HashMap<String, Object>> result) {
            super.onPostExecute(result);
        if(result!=null)
        {
             adapterfirst = new Questionadapter(historylist.this,result);
             listviewfirst = (RefeshListView) findViewById(R.id.listquestion);
             listviewfirst.setAdapter(adapterfirst);
                adapterfirst.notifyDataSetChanged();
        }
         yourlistView.postDelayed(new Runnable() {

            @Override
            public void run() {
                 new RefreshTask().execute();  
            }
        }, 1000);

}
        }
    }

Hi Priya ! try this procedure! It is always a good way to pass the objects to onpostExecute as a parameter! Hope this will fix your issue!

嗨Priya !试试这个程序!它始终是将对象传递到onpostExecute作为参数的好方法!希望这能解决你的问题!

#5


0  

This may not be exact . but try to modify accordingly,

这可能并不准确。但是试着相应地修改,

 public void Callpagedetails() {

    try
        {
     URL = http://history_thread.php?token_id=" + Token + "&p=" +                       page_count;                   
            Log.d("TAG", "API URL IS " + URL);

            HttpClient mHttpClient = new DefaultHttpClient();

            HttpGet mGetMethod = new HttpGet(URL);

            HttpResponse mReponseMessage = mHttpClient.execute(mGetMethod);

            String Response = EntityUtils.toString(mReponseMessage.getEntity());

            Log.d("TAG", "O/P Response is " + Response);

            JSONArray responseObject = new JSONArray(Response);

                      for(int i=0; i < responseObject.length(); i++)

                {
                    obj = responseObject.getJSONObject(i);


                }
  }
catch(Exception e)
{
 System.out.println(e);
}

}

and , in your

,在你的

doInBackground() {
   ....
 CallPageDetails();

}

and,

而且,

 onPostExecute() {
                    if(yourListView!=null) {
                  listhashmap = new HashMap<String, Object>();

                    listhashmap.put("text",obj.getString("text"));

            here YOU WILL BE storing the item in arraylist arraylistitems
  &&  also set, 
            yourAdapter.notifyDataSetChanged();
                 }
        .....
        .....


}

*-> Try at your Own Risk.

-冒你自己的风险。

#6


0  

arraylist hold object refrence so your list hold only last value becuase you are new object on same memory locaion.

arraylist保存对象的折射,所以你的列表只保存最后一个值,因为你是同一个内存上的新对象。

for (int i = 0; i < responseObject.length(); i++) {
    obj = r esponseObject.getJSONObject(i);
    listhashmap = new HashMap < String, Object > ();  // problems code
    listhashmap.put("text", obj.getString("text"));
}

replace with

替换为

for (int i = 0; i < responseObject.length(); i++) {
    obj = r esponseObject.getJSONObject(i);
 **HashMap<String, Object> listhashmap = new HashMap<String, Object>();**

                        listhashmap.put("text",obj.getString("text"));

                here i am storing the item in arraylist arraylistitems
                         rraylistitem.put(listhashmap);
}