title: Start app and load data with viewpager AsyncTask
body: <p>I'm using AsyncTask with Viewpager and so far everything is working properly. When I run my application everything works fine and displays the data by sliding from one column to another. But when you start the app the data in that first column does not appear. I have tried in the onCreate insert the following code:</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      url = "1";
      layout = R.id.lista;
      empty = R.id.empty;
      DownloadJSON newTask = new DownloadJSON(url,layout,empty);
      newTask.execute();
      ...
  </code></pre>
  
  <p>It does not work. The app is closed. How do I can load this data when the app starts?</p>
  
  <p>My code:</p>
  
  <pre><code>public class MainActivity extends Activity {
  
  ViewPager vp;
  private vpAdapter myAdapter;
  ListViewAdapter adapter;
  ListView listview;
  String url;
  int layout;
  int empty;
  ArrayList&lt;HashMap&lt;String, String&gt;&gt; arraylist;
  
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      ctx = this;
      setContentView(R.layout.activity_main);
  
   vp = (ViewPager) findViewById(R.id.pager);
      myAdapter = new vpAdapter();
      vp.setAdapter(myAdapter);
  
  
  vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
          @Override
          public void onPageScrolled(int i, float v, int i2) {
  
          }
  
          @Override
          public void onPageSelected(int i) {
                                   //CASE 0 = FIRST PAGE
              switch (i) {
              case 0:
                  url = "1";
                  layout = R.id.lista;
                  empty = R.id.empty;
                  DownloadJSON newTask = new DownloadJSON(url,layout,empty);
                  newTask.execute();
                  break;
              default:
                  break;
              case 1:
                  url = "2";
                  layout = R.id.lista2;
                  empty = R.id.empty2;
                  DownloadJSON newTask2 = new DownloadJSON(url,layout,empty);
                  newTask2.execute();
                  break;
              case 2:
                  url = "3";
                  layout = R.id.lista3;
                  empty = R.id.empty3;
                  DownloadJSON newTask3 = new DownloadJSON(url,layout,empty);
                  newTask3.execute();
                  break;
              case 3:
                  url = "4";
                  layout = R.id.lista4;
                  empty = R.id.empty4;
                  DownloadJSON newTask4 = new DownloadJSON(url,layout,empty);
                  newTask4.execute();
                  break;
              case 4:
                  url = "5";
                  layout = R.id.lista5;
                  empty = R.id.empty5;
                  DownloadJSON newTask5 = new DownloadJSON(url,layout,empty);
                  newTask5.execute();
                  break;
              case 5:
                  url = "6";
                  layout = R.id.lista6;
                  empty = R.id.empty6;
                  DownloadJSON newTask6 = new DownloadJSON(url,layout,empty);
                  newTask6.execute();
                  break;
              }   
          }
  
          @Override
          public void onPageScrollStateChanged(int arg0) {
              // TODO Auto-generated method stub
  
          }
      });
  
      private class DownloadJSON extends AsyncTask&lt;Void, Void, Void&gt; {
      String givemeurl;
      int givemelayout;
      int givemeempty;
  
      public DownloadJSON(String url, int layout, int empty) {
          this.givemeurl = url;
          this.givemelayout = layout;
          this.givemeempty = empty;
      }
  
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
  
          ListView listview = (ListView) findViewById(layout);
          if(listview.getCount()==0){
              mProgressDialog = new ProgressDialog(MainActivity.this);
              mProgressDialog.setMessage("Cargando...");
              mProgressDialog.setIndeterminate(false);
              mProgressDialog.show();
          }else{
              cancel(true);
              Log.e("CANCELADO","CANCELADO");
          }
  
      }
  
      @Override
      protected Void doInBackground(Void... params) {
          // Create an array
          arraylist = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
          // Retrieve JSON Objects from the given URL address
          jsonobject = JSONfunctions
                  .getJSONfromURL("URL");
  
          if(jsonobject != null){         
              try {
                  // Locate the array name in JSON
                  jsonarray = jsonobject.getJSONArray("productos");
  
                  for (int i = 0; i &lt; jsonarray.length(); i++) {
                      HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
                      jsonobject = jsonarray.getJSONObject(i);
                      // Retrive JSON Objects
                      map.put("imagen", jsonobject.getString("imagen"));
                      map.put("nombre", jsonobject.getString("nombre"));
                      map.put("total", jsonobject.getString("total"));
                      map.put("empresa", jsonobject.getString("empresa"));
                      map.put("unidades", jsonobject.getString("unidades"));
                      map.put("precionuevo", jsonobject.getString("precionuevo")+" €");
                      map.put("precioantiguo", jsonobject.getString("precioantiguo")+" €");
                      map.put("descripcion", jsonobject.getString("descripcion"));
                      map.put("direccion", jsonobject.getString("direccion"));
                      map.put("telefono", jsonobject.getString("telefono"));
                      map.put("latitud", jsonobject.getString("latitud"));
                      map.put("longitud", jsonobject.getString("longitud"));
                      map.put("codeqr", jsonobject.getString("codeqr"));
                      arraylist.add(map);
                  }
  
              } catch (JSONException e) {
                  Log.e("Error", e.getMessage());
                  //e.printStackTrace();
              } catch (Exception e) {
                  Log.e("Error", e.getMessage());         
              }
          }else{
              //Log.e("Response","No data");
          }
  
          return null;
      }
  
      @Override
      protected void onPostExecute(Void args) {
          listview = (ListView) findViewById(givemelayout);
          listview.setEmptyView(findViewById(givemeempty));
          adapter = new ListViewAdapter(MainActivity.this, arraylist);
          listview.setAdapter(adapter);
          mProgressDialog.dismiss();
          //Log.e("",""+listview.getCount());
      }
  }
  </code></pre>