-3

I am fetching data from url and storing it in arraylist and displaying that content in tab(using action bar).The problem is app is crashing when internet connection gets enabled after it gets disabled because size of arraylist its getting is 0. The code that I have implemented is-

class stopOffer extends AsyncTask<String,String,ArrayList<HashMap<String,String>>> 
        {
            int flag=1;

            @Override
            protected void onPreExecute()
            {
                super.onPreExecute();
            }


            @Override
            protected ArrayList<HashMap<String,String>> doInBackground(String... urls) 
            {
                stopOffer=new ArrayList<HashMap<String,String>>();
                String url = urls[0];
                try 
                {
                    for (int k= 0; k<info.length; k++)
                    {
                        if (info[k].getState() == NetworkInfo.State.CONNECTED)
                          {
                            InputStream in = new java.net.URL(url).openStream();

                            BufferedReader reader = null;
                            reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));

                            StringBuilder sb = new StringBuilder();
                            String append = sb.append(reader.readLine() + "\n").toString();

                            getDataBeforeHash = append.split("#");
                            len = getDataBeforeHash.length;
                            getDataBeforeComma=new String[len][];

                            for (int i = 0; i < len; i++) 
                            {
                                getDataBeforeComma[i] = getDataBeforeHash[i].split(",");
                                lenBeforeComma=getDataBeforeComma[i].length;

                                HashMap<String,String> stopOfferMap=new HashMap<String,String>();

                                for(int j=0;j<getDataBeforeComma[i].length;j++)
                                {
                                    if(j==0)
                                    {
                                        stopOfferMap.put("id",getDataBeforeComma[i][j]);
                                    }
                                    else if(j==1)
                                    {
                                        stopOfferMap.put("name",getDataBeforeComma[i][j]);
                                    }
                                    else if(j==2)
                                    {
                                        stopOfferMap.put("count",getDataBeforeComma[i][j]);
                                    }
                                    else if(j==3)
                                    {
                                        stopOfferMap.put("amount",getDataBeforeComma[i][j]);
                                    }
                                }
                                stopOffer.add(stopOfferMap);
                            }
                         }
                    }
                }
                catch (IOException e) 
                {
                    flag=0;
                    // TODO Auto-generated catch block
                    e.printStackTrace();

                }
                return stopOffer;

              }

              @Override
              protected void onPostExecute(ArrayList<HashMap<String,String>> stopOffer) 
              {
                  if(flag==0)
                    {
                      getActivity().runOnUiThread(new Runnable() {
                            public void run() {
                                Toast.makeText(getActivity(),
                                        "Unable to connect to server",
                                        Toast.LENGTH_SHORT).show();
                            }
                        });

                    }
                    else
                    {

                            tr=new TableRow[len];
                            id=new String[len];
                            name=new String[len];
                            Log.e("size stop",""+stopOffer.size());
                            for (int i = 0; i < len; i++) 
                            {
                                id[i] = stopOffer.get(i).get("id");
                                name[i] = stopOffer.get(i).get("name");
                                cnt = stopOffer.get(i).get("count");
                                amt = stopOffer.get(i).get("amount");

                                idOffer=stopOffer.get(i).get("id");
                                nameOffer=stopOffer.get(i).get("name");
                                tr[i] = new TableRow(getActivity());
                                tr[i].setId(i);

                                if (i % 2 == 0)
                                    tr[i].setBackgroundColor(Color.WHITE);
                                else
                                    tr[i].setBackgroundColor(Color.parseColor("#b6d6eb"));

                                tr[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

                                TextView OfferId1 = new TextView(getActivity());
                                OfferId1.setText(id[i]);
                                OfferId1.setTextColor(Color.BLACK);
                                OfferId1.setPadding(5, 5, 5, 5);
                                tr[i].addView(OfferId1);// add the column to the table row here

                                TextView OfferName = new TextView(getActivity());
                                OfferName.setText(name[i]); // set the text for the header
                                OfferName.setTextColor(Color.BLACK); // set the color
                                OfferName.setPadding(5, 5, 5, 5); // set the padding (if
                                                        // required)
                                tr[i].addView(OfferName); // add the column to the table row

                                TextView totCountRunning = new TextView(getActivity());
                                totCountRunning.setText(cnt); // set the text for the header
                                totCountRunning.setTextColor(Color.BLACK); // set the color
                                totCountRunning.setPadding(5, 5, 5, 5); // set the padding
                                                            // (if
                                                            // required)
                                tr[i].addView(totCountRunning); // add the column to the table
                                                    // row here

                                TextView totAmtRunning = new TextView(getActivity());
                                totAmtRunning.setText(amt); // set the text for the header
                                totAmtRunning.setTextColor(Color.BLACK); // set the color
                                totAmtRunning.setPadding(5, 5, 5, 5); // set the padding (if
                                                            // required)
                                tr[i].addView(totAmtRunning); // add the column to the table
                                                // row here

                                tl.addView(tr[i], new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
                                db.insertStopDetails(idOffer, nameOffer, cnt, amt);
                                tr[i].setOnLongClickListener(new OnLongClickListener() 
                                {
                                    @Override
                                    public boolean onLongClick(View v) 
                                    {
                                            // TODO Auto-generated method stub
                                        idTr=v.getId();
                                        dialog=new Dialog(getActivity());
                                        dialog.setContentView(R.layout.enable_disable_offer);
                                        CheckBox cb=(CheckBox)dialog.findViewById(R.id.checkBoxEnableDisable);
                                        final Button ok=(Button)dialog.findViewById(R.id.buttonOK);
                                        final Button cancel=(Button)dialog.findViewById(R.id.buttonCancel);
                                        cb.setText("Enable Offer:"+" "+name[idTr]);
                                        dialog.setTitle("Enable Offer");

                                        if(cb.isChecked())
                                        {
                                            ok.setOnClickListener(new OnClickListener()
                                            {
                                                @Override
                                                public void onClick(View v) 
                                                {
                                                    setConnection();
                                                    // add your data
                                                    nameValuePairs = new ArrayList<NameValuePair>(3);
                                                    nameValuePairs.add(new BasicNameValuePair("flag", "1"));
                                                    nameValuePairs.add(new BasicNameValuePair("offerName",id[idTr]));
                                                    nameValuePairs.add(new BasicNameValuePair("OfferStatus","1"));
                                                    enableOffer();
                                                    dialog.cancel();
                                                }
                                            });
                                            cancel.setOnClickListener(new OnClickListener()
                                            {
                                                @Override
                                                public void onClick(View v) 
                                                {
                                                    dialog.cancel();
                                                }
                                            });
                                        }
                                        else
                                        {
                                            ok.setOnClickListener(new OnClickListener()
                                            {
                                                @Override
                                                public void onClick(View v) 
                                                {
                                                    dialog.cancel();
                                                } 
                                            });
                                            cancel.setOnClickListener(new OnClickListener()
                                            {
                                                @Override
                                                public void onClick(View v) 
                                                {
                                                    dialog.cancel();
                                                }
                                            });
                                        }
                                        dialog.show();
                                        return true;
                                    }
                                });
                            }

                    }
                }
              }

I am calling this class from onCreateView if internet is available.

Logcat-

10-13 13:44:37.059: E/AndroidRuntime(4677): FATAL EXCEPTION: main
10-13 13:44:37.059: E/AndroidRuntime(4677): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
10-13 13:44:37.059: E/AndroidRuntime(4677):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
10-13 13:44:37.059: E/AndroidRuntime(4677):     at java.util.ArrayList.get(ArrayList.java:304)
10-13 13:44:37.059: E/AndroidRuntime(4677):     at info.rh.dashboard.RunningOfferFragment$dataRunningStopOffer.onPostExecute(RunningOfferFragment.java:635)
10-13 13:44:37.059: E/AndroidRuntime(4677):     at info.rh.dashboard.RunningOfferFragment$dataRunningStopOffer.onPostExecute(RunningOfferFragment.java:1)
10-13 13:44:37.059: E/AndroidRuntime(4677):     at android.os.AsyncTask.finish(AsyncTask.java:631)
10-13 13:44:37.059: E/AndroidRuntime(4677):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-13 13:44:37.059: E/AndroidRuntime(4677):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-13 13:44:37.059: E/AndroidRuntime(4677):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 13:44:37.059: E/AndroidRuntime(4677):     at android.os.Looper.loop(Looper.java:176)
10-13 13:44:37.059: E/AndroidRuntime(4677):     at android.app.ActivityThread.main(ActivityThread.java:5319)
10-13 13:44:37.059: E/AndroidRuntime(4677):     at java.lang.reflect.Method.invokeNative(Native Method)
10-13 13:44:37.059: E/AndroidRuntime(4677):     at java.lang.reflect.Method.invoke(Method.java:511)
10-13 13:44:37.059: E/AndroidRuntime(4677):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
10-13 13:44:37.059: E/AndroidRuntime(4677):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
10-13 13:44:37.059: E/AndroidRuntime(4677):     at dalvik.system.NativeStart.main(Native Method)
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
Anushka
  • 73
  • 1
  • 13

1 Answers1

0

Just check you added the permission in manifest. If not Add the INTERNET permission to your manifest file.

You have to add this line:

<uses-permission android:name="android.permission.INTERNET" />

outside the application tag in your AndroidManifest.xml

khurram
  • 1,362
  • 13
  • 24
  • @khurram- I have already added it.Its fetching data easily if there is no interruption in internet connection – Anushka Oct 13 '14 at 09:20
  • for (int i = 0; i < len; i++) replace this line with for (int i = 0; i < stopOffer.size(); i++) – khurram Oct 13 '14 at 09:24