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

时间:2014-01-20 13:01:07

标签: android android-listview pull-to-refresh

我在android Pull to refresh listview.Its工作正常,但在刷新listview时,我正在尝试向下滚动列表。我收到此错误:

这是我的代码:

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

    }

arraylistitems是ArrayList<HashMap<String, Object>> arraylistitems;

其中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)

有人可以帮助我吗?感谢。

6 个答案:

答案 0 :(得分:3)

我没有尝试,但确定它可能对你有所帮助。

                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"));

                }

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

在Callpagedetails上,查看instanced responseObject之后的for循环。

保留初始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"));
}

答案 3 :(得分:0)

受保护的类RefreshTask扩展了AsyncTask&gt;&gt; {

        @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);

}
        }
    }

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

答案 4 :(得分:0)

这可能不准确。但请尝试相应地修改,

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

}

,并在您的

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

}

 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();
                 }
        .....
        .....


}

* - &GT;尝试自己的风险。

答案 5 :(得分:0)

arraylist hold object refrence所以你的列表只保留最后一个值,因为你是同一个内存位置的新对象。

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

替换为

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