Indexoutofbounds异常Inavalid索引0和大小为0

时间:2015-03-21 06:28:49

标签: android arrays android-listview arraylist geojson

我是android开发的新手。我收到此错误indexoutofbounds异常Inavalid索引0和大小为0.

03-21 02:19:44.530: E/AndroidRuntime(2693): FATAL EXCEPTION: main
   03-21 02:19:44.530: E/AndroidRuntime(2693):   java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
   03-21 02:19:44.530: E/AndroidRuntime(2693):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
   03-21 02:19:44.530: E/AndroidRuntime(2693):  at java.util.ArrayList.get(ArrayList.java:308)
   03-21 02:19:44.530: E/AndroidRuntime(2693):  at com.example.listcheck.ListActivity.GetSearchResults2(ListActivity.java:284)
   03-21 02:19:44.530: E/AndroidRuntime(2693):  at com.example.listcheck.ListActivity.access$0(ListActivity.java:267)
   03-21 02:19:44.530: E/AndroidRuntime(2693):  at com.example.listcheck.ListActivity$LoadData.onPostExecute(ListActivity.java:260)
   03-21 02:19:44.530: E/AndroidRuntime(2693):  at com.example.listcheck.ListActivity$LoadData.onPostExecute(ListActivity.java:1)
   03-21 02:19:44.530: E/AndroidRuntime(2693):  at android.os.AsyncTask.finish(AsyncTask.java:631)
   03-21 02:19:44.530: E/AndroidRuntime(2693):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
   03-21 02:19:44.530: E/AndroidRuntime(2693):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
   03-21 02:19:44.530: E/AndroidRuntime(2693):  at android.os.Handler.dispatchMessage(Handler.java:99)
   03-21 02:19:44.530: E/AndroidRuntime(2693):  at android.os.Looper.loop(Looper.java:137)
   03-21 02:19:44.530: E/AndroidRuntime(2693):  at android.app.ActivityThread.main(ActivityThread.java:5103)
   03-21 02:19:44.530: E/AndroidRuntime(2693):  at java.lang.reflect.Method.invokeNative(Native Method)
   03-21 02:19:44.530: E/AndroidRuntime(2693):  at java.lang.reflect.Method.invoke(Method.java:525)
   03-21 02:19:44.530: E/AndroidRuntime(2693):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
   03-21 02:19:44.530: E/AndroidRuntime(2693):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
   03-21 02:19:44.530: E/AndroidRuntime(2693):  at dalvik.system.NativeStart.main(Native Method)

这是我的代码:

public class ListActivity extends Activity {

ListView listService;
String url = "http://textbookmarket.in/demo/tick-tack-toe-spa/webservices/index.php?method=getAllServices";

ArrayAdapter<String> adapterService;
ProgressDialog progressDialog;

ListView serviceListData;

ArrayList<String> arrayServiceTitle, arrayServiceDes,
        arrayServicesthirtynormal, arrayservicesthirtymember,
        arrayServicessixtynormal, arrayservicessixtytymember,
        arrayservicesningtytynormal, arrayservicesningtytymember;
ArrayList<Integer> arrayServiceId;

ImageView btnBackToMediaMainActivity;
LinearLayout btnContactUs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);

    createObjs();

    listService = (ListView) findViewById(R.id.mlistview);

    // btnBackToMediaMainActivity = (ImageView)
    // findViewById(R.id.btnBackToMediaMainActivity);

    ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
    Boolean isInternetPresent = cd.isConnectingToInternet();

    if (isInternetPresent) {
        new LoadData().execute();
    } else {

    }


 }

 public class ConnectionDetector {

    Context _context;

    public ConnectionDetector(Context context) {
        this._context = context;
    }

    public boolean isConnectingToInternet() {
        ConnectivityManager connectivity = (ConnectivityManager) getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }

        }
        return false;
    }
  }

   private void createObjs() {

    arrayServiceId = new ArrayList<Integer>();
    arrayServiceTitle = new ArrayList<String>();
    // arrayServiceImage = new ArrayList<String>();

    arrayServiceDes = new ArrayList<String>();
    arrayServicesthirtynormal = new ArrayList<String>();
    arrayservicesthirtymember = new ArrayList<String>();

    arrayServicessixtynormal = new ArrayList<String>();
    arrayservicessixtytymember = new ArrayList<String>();

    arrayservicesningtytynormal = new ArrayList<String>();

    arrayservicesningtytymember = new ArrayList<String>();

   }

   public class LoadData extends AsyncTask<String, Void, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = ProgressDialog.show(ListActivity.this, "",
                "Please wait", true);
        progressDialog.setCancelable(true);

    }

    @Override
    protected String doInBackground(String... params) {
        // Fetch Top Articles

        String mResponse = fetchJsonUrl(url);

        Log.v("log", " resultsEventsAll --> " + mResponse);

        try {
            JSONObject jsonObject = new JSONObject(mResponse);
            String status = jsonObject.optString("status");

            if(status != null && status.equals("1")) {
                JSONObject jsonResultObj = jsonObject.optJSONObject("result");
                if(jsonResultObj != null) {

                    JSONArray jsonServicesArray = jsonResultObj.optJSONArray("services");
                    if(jsonServicesArray != null && jsonServicesArray.length() > 0) {
                        //resultServiceArrayList = new ArrayList<ResultService>(jsonServicesArray.length());
                        for(int i = 0; i < jsonServicesArray.length(); i++) {
                            JSONObject jsonObject2 = jsonServicesArray.optJSONObject(i);
                            if(jsonObject2 != null) {
                                ListModel resultService = new ListModel ();
                                resultService.setServicesId(jsonObject2.getInt("service_id"));
                                resultService.setServicesTitle(jsonObject2.optString("name"));
                                resultService.setServicesDes(jsonObject2.optString("description"));


                                JSONObject json30 = jsonObject2.optJSONObject("30_mins");
                                if(json30 != null) {
                                    ListModel resultServiceThirty = new ListModel();
                                    resultServiceThirty.setServicesThirtynormal(json30.optString("normal"));
                                    resultServiceThirty.setServicesThirtymember(json30.optString("member"));
                                //  resultService.setResultServiceThirty(resultServiceThirty);
                                } 

                                JSONObject json60 = jsonObject2.optJSONObject("60_mins");
                                if(json30 != null) {
                                    ListModel resultServiceSixtyty = new ListModel();
                                    resultServiceSixtyty.setServicesThirtynormal(json60.optString("normal"));
                                    resultServiceSixtyty.setServicesThirtymember(json60.optString("member"));
                                //  resultService.setResultServiceThirty(resultServiceThirty);
                                } 
                                JSONObject json90 = jsonObject2.optJSONObject("90_mins");
                                if(json30 != null) {
                                    ListModel resultServiceNinty = new ListModel();
                                    resultServiceNinty.setServicesThirtynormal(json90.optString("normal"));
                                    resultServiceNinty.setServicesThirtymember(json90.optString("member"));
                                //  resultService.setResultServiceThirty(resultServiceThirty);
                                } 

                            }


                arrayServiceId.add(jsonObject2.getInt("service_id"));
                arrayServiceTitle.add(jsonObject2.getString("name"));
                arrayServiceDes.add(jsonObject2.getString("description"));

                arrayServicesthirtynormal.add(jsonObject2.getString("normal"));
                arrayservicesthirtymember.add(jsonObject2.getString("member"));

                arrayServicessixtynormal.add(jsonObject2.getString("normal"));  
                arrayservicessixtytymember.add(jsonObject2.getString("member"));

                arrayservicesningtytynormal.add(jsonObject2.getString("normal"));
                arrayservicesningtytymember.add(jsonObject2.getString("member"));
                        }
                    }
                }
                        }
        } catch (Exception e) {

        }

        return mResponse;
    }

    public String getMonth(int month) {
        return new DateFormatSymbols().getMonths()[month - 1];
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        progressDialog.dismiss();

        ArrayList<ListModel> searchResults2 = GetSearchResults2();
        listService.setAdapter(new ListAdapter(getApplicationContext(),
                searchResults2));

      }
   }

    private ArrayList<ListModel> GetSearchResults2() {
    ArrayList<ListModel> results = new ArrayList<ListModel>();

    Log.v("log", " Test Results GetSearch ");

    Log.v("log", " services Size " + arrayServiceId.size());

    for (int i = 0; i < arrayServiceId.size(); i++) {

        Log.v("log", " Test " + i);

        ListModel sr1 = new ListModel();
        sr1.setServicesId(arrayServiceId.get(i));
        sr1.setServicesTitle(arrayServiceTitle.get(i));
        // sr1.setServicesImage(arrayServiceImage.get(i));
        sr1.setServicesDes(arrayServiceDes.get(i));

        sr1.setServicesThirtynormal(arrayServicesthirtynormal.get(i));
        sr1.setServicesThirtynormal(arrayServicesthirtynormal.get(i));

        sr1.setServicesSixtynormal(arrayServicessixtynormal.get(i));
        sr1.setServicesSixtymember(arrayservicessixtytymember.get(i));

        sr1.setServicesNingtynormal(arrayservicesningtytynormal.get(i));
        sr1.setServicesNingtymember(arrayservicesningtytymember.get(i));

        results.add(sr1);
    }

    return results;
   }

     public String fetchJsonUrl(String url) {
    // TODO Auto-generated method stub
    StringBuffer sb = new StringBuffer();
    try {
        URL mUrl = new URL(url);

        URLConnection uc = mUrl.openConnection();
        BufferedReader buffer = new BufferedReader(new InputStreamReader(
                uc.getInputStream()));
        String line = "";
        while ((line = buffer.readLine()) != null) {

            sb.append(line + "\n");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
    }

  }

2 个答案:

答案 0 :(得分:0)

基本上这个异常意味着其中一个列表的大小为0且serviceId大小大于0,所以当你注意到你正在尝试访问空列表的第0个元素时它会抛出异常。

答案 1 :(得分:0)

基于异常,似乎以下任何一个arraylist都是空的,你试图从空数组中检索值 在onPostExecute方法

    sr1.setServicesTitle(arrayServiceTitle.get(i)) ; 
// sr1.setServicesImage(arrayServiceImage.get(i));
     sr1.setServicesDes(arrayServiceDes.get(i));
     sr1.setServicesThirtynormal(arrayServicesthirtynormal.get(i));
     sr1.setServicesThirtynormal(arrayServicesthirtynormal.get(i));
     sr1.setServicesSixtynormal(arrayServicessixtynormal.get(i));
     sr1.setServicesSixtymember(arrayservicessixtytymember.get(i));
     sr1.setServicesNingtynormal(arrayservicesningtytynormal.get(i));
     sr1.setServicesNingtymember(arrayservicesningtytymember.get(i));