Domanda

I am developing an android application which requires to fetch data items from json to ViewPager

For that i had used following Code:

    public class GalleryUrlActivity extends Activity {

        private GalleryViewPager mViewPager;
        String anim_id,album_id,anim_name,anim_thumb,anim_url;
        ArrayList<HashMap<String, String>> albumList;
        int global_position=0; 
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            albumList = new ArrayList<HashMap<String, String>>();

            new AsycLove().execute();
        }

        class AsycLove extends AsyncTask<String, String, String>     
        {
            ProgressDialog progressDialog;      

            @Override
            protected void onPreExecute() 
            {
                super.onPreExecute();            
                progressDialog = new ProgressDialog(GalleryUrlActivity.this);
                progressDialog.setTitle("Loading");
                progressDialog.setMessage("Please wait");
                progressDialog.setCancelable(false);
                progressDialog.setIndeterminate(true);
                progressDialog.show();
            }

            @Override
            protected String doInBackground(String... aurl)        
            {           
                try 
                {
                    HttpPost postMethod = null;

                    postMethod = new HttpPost("http://demo1.idevtechnolabs.com/smartecard/love.php");

                    BufferedReader bufferedReader = null;

                    HttpClient client = new DefaultHttpClient();
                    HttpResponse response = null;

                    response = client.execute(postMethod);
                    final int statusCode = response.getStatusLine().getStatusCode();

                    Log.v("Album ::","Response:::--->"+response.toString());
                    Log.v("Album ::","Status Code:::--->"+statusCode);

                    bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                    StringBuffer stringBuffer = new StringBuffer("");
                    String line = "";
                    String LineSeparator = System.getProperty("line.separator");
                    while ((line = bufferedReader.readLine()) != null) 
                    {
                        stringBuffer.append(line + LineSeparator); 
                    }
                    bufferedReader.close(); 

                    //-------------CONVERT DATA TO JSON---------------------------------

                    try 
                    {
                        String myjsonstring = stringBuffer.toString();

                        JSONArray jsonArray = new JSONArray(myjsonstring);

                        JSONObject jsonObj = null;

                        albumList.clear();

                        jsonObj = jsonArray.getJSONObject(0);   


                        for(int i=0; i<jsonArray.length();i++)
                        {
                                jsonObj = jsonArray.getJSONObject(i);                           

                                anim_id = jsonObj.getString("animation_id");      
                                album_id = jsonObj.getString("album_id");
                                anim_name = jsonObj.getString("animation_name");      
                                anim_thumb= jsonObj.getString("animation_thumb");
                                anim_url = jsonObj.getString("animation_url");

                                anim_url=anim_url.replaceAll("\"","");


                                Log.v("Anim URL","Anim URL::"+anim_url);
                                Log.v("Anim Name","Anim Name::"+anim_name);

                                HashMap<String, String> tmp_album = new HashMap<String, String>();
                                tmp_album.put("anim_id", anim_id);
                                tmp_album.put("anim_thumb", anim_thumb);
                                tmp_album.put("anim_url",anim_url);

                                albumList.add(tmp_album);
                                Log.v("Animation URL Position","Animation::"+global_position+"::"+anim_url);

                        }

                    } 
                    catch (Exception e) 
                    {
                        Log.v("Home ::","Call JSON Exception in get Album in List--->"+e.toString());
                        e.printStackTrace();
                    }
                } 
                catch (IOException e) 
                {
                    Log.v("Exception: Get get Album in List","Name-"+e.toString());
                    e.printStackTrace();
                }

                return "0";
            }

            @Override
            protected void onPostExecute(String code) 
            {   
                Toast.makeText(getApplicationContext(), "Image URL Call Successfully", Toast.LENGTH_LONG).show();
                Log.v("Album","Album List::"+albumList);

                String urls[] = null;
                for(int p=0;p<=albumList.size();p++)
                {
                    urls[p]=albumList.get(p).get("anim_thumb");
                }

                List<String> items = new ArrayList<String>();
                Collections.addAll(items, urls);

                UrlPagerAdapter pagerAdapter = new UrlPagerAdapter(getApplicationContext(), items);
                pagerAdapter.setOnItemChangeListener(new OnItemChangeListener()
                {
                    @Override
                    public void onItemChange(int currentPosition)
                    {
                        Toast.makeText(GalleryUrlActivity.this, "Current item is " + currentPosition, Toast.LENGTH_SHORT).show();
                    }
                });

                mViewPager = (GalleryViewPager)findViewById(R.id.viewer);
                mViewPager.setOffscreenPageLimit(3);
                mViewPager.setAdapter(pagerAdapter);

                progressDialog.dismiss();
                progressDialog = null;

            }
        }
    }

In that I am getting Following Error:

    FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
    at java.util.ArrayList.get(ArrayList.java:304)
    at ru.truba.touchgallery.GalleryUrlActivity$AsycLove.onPostExecute(GalleryUrlActivity.java:176)
    at ru.truba.touchgallery.GalleryUrlActivity$AsycLove.onPostExecute(GalleryUrlActivity.java:1)
    at android.os.AsyncTask.finish(AsyncTask.java:631)
    at android.os.AsyncTask.access$600(AsyncTask.java:177)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5371)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    at dalvik.system.NativeStart.main(Native Method)

Can Any one tell me please why it is cause of?

Thank you

È stato utile?

Soluzione

You didn't initialized your albumList. You have just declared a variable for that.

  ArrayList<HashMap<String, String>> albumList;

Now initialize it onCreate() method.

  albumList = new ArrayList<HashMap<String, String>>();

Altri suggerimenti

try this way,hope this will help you...

Replace albumList.clear(); with

if(albumList!=null) {
  albumList.clear();
}else {
  albumList = new ArrayList<HashMap<String, String>>();
}

And

Replace this code:

String urls[] = null;
for(int p=0;p<=albumList.size();p++){
   urls[p]=albumList.get(p).get("anim_thumb");
}

with this code

String urls[] = new String[albumList.size()];
for(int p=0;p<albumList.size();p++){
   urls[p]=albumList.get(p).get("anim_thumb");
}

I think you forget to declare String array like

String[] urls = new String[albumList.size()];

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top