title: Splash Screen with AsyncTask
body: <p>I want to implement a splash screen into my app. I already did this. But at the moment it just waits 3 seconds and then calls the MainActivity class. The problem with that is that i have data to load and with the current setup the user have to waits two times. I want a splash screen that loads all the data. I have a MainActivity class where everything happens and I have my SplashScreen class. </p>
  
  <p>The method I want to be run in the background is in my MainClass. So basically I have my splash screen class like that</p>
  
  <pre><code>public class SplashScreenActivity extends AsyncTask&lt;Void, Void, Void&gt; {
  
  @Override
  protected Void doInBackground(Void... params) {
      // TODO Auto-generated method stub
      return null;
  }
  
  protected void onPostExecute(Void... params) {
      // TODO Auto-generated method stub
  
  }
  
  protected void onCancelled() {
      // TODO Auto-generated method stub
  }
  
  }
  </code></pre>
  
  <p>I also implemented but not copied the imports and packages, so that shouldn't be a problem. Now, if I understood correctly I need to write the task that should be done into the doInBackground method. So I basically have to call the method from my other activity class, right?</p>
  
  <pre><code>public MainActivity mA = new MainActivity();
  </code></pre>
  
  <p>and then in the method I would write <code>mA.parseXMLfromURL();</code></p>
  
  <p>And afterwards I would start an intent of the main class into the onPostExecute-method like this?</p>
  
  <pre><code>protected void onPostExecute(Void... params) {
      Intent mainClass = new Intent(SplashScreenActivity.this, MainActivity.class);
      startActivity(mainClass);
  
  }
  </code></pre>
  
  <p>If more information is needed I will gladly elaborate further.</p>
  
  <p><strong>UPDATES</strong></p>
  
  <p>Well, after your comments I tried it a bit differently. </p>
  
  <p>This is my oncreate method</p>
  
  <pre><code>    public void onCreate(Bundle savedInstanceState) {
      StrictMode.setThreadPolicy(policy);
      super.onCreate(savedInstanceState);
      sv      = new ScrollView(this);
      layout = new LinearLayout(this);
      layout.setOrientation(LinearLayout.VERTICAL);
      sv.addView(layout);
      setContentView(sv);     
  
      new SplashScreenActivity().execute("URL TO FILE");
  }
  </code></pre>
  
  <p>And this is the SplashScreenActivity</p>
  
  <pre><code>public class SplashScreenActivity extends AsyncTask&lt;String, Void, Void&gt; {
  public MainActivity mA = new MainActivity();
  
  protected void onPostExecute(Void... params) {
  }
  
  protected void onCancelled() {
      // TODO Auto-generated method stub
  }
  
  @Override
  protected Void doInBackground(String... params) {
      mA.parseXMLfromURL(params);
      return null;
  }
  </code></pre>
  
  <p><code>}</code></p>
  
  <p>But this just returns a blank screen. However if I call the parseXMLfromURL in my main activity it works just fine.</p>
  
  <p>@Raghunandan said in comments that I wrongly created the instance of the class. Would be glad if you could elaborate your answer.</p>
  
  <p><strong>UPDATE NUMBER TWO</strong></p>
  
  <p>Current SplashScreen-Code is the following:</p>
  
  <p>package de.activevalue.T1000flies;</p>
  
  <pre><code>import android.app.Activity;
  import android.content.Intent;
  import android.os.AsyncTask;
  import android.os.Bundle;
  
  public class SplashScreenActivity extends Activity{
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.splash_screen);
  
      new mTask().execute();
  }
  
  private class mTask extends AsyncTask&lt;Void, Void, Void&gt;{
  
      MainActivity mA = new MainActivity();
      @Override
      protected Void doInBackground(Void... arg0) {
          mA.parseXML();
          return null;
      }
  
      protected void onPostExecute(Void... params){
          Intent i = new Intent(SplashScreenActivity.this, MainActivity.class);
          startActivity(i);
          finish();
      }
  
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>With that code the app just stucks at the Splash-Screen. It seeems like there is a problem with my XML-Parsing. Here is the XML-Parsing-code. Note that it works without any problems, when I just start the main activity wihtout the splash screen</p>
  
  <p><strong>UPDATE NUMBER THREE</strong></p>
  
  <p>I just started to debug by making breakpoints line per line. It jumps out at this line</p>
  
  <pre><code>rankingDate [k]             = new TextView(this);
  </code></pre>
  
  <p>Rest of the code</p>
  
  <pre><code>for(int k = 0; k &lt; metaList.getLength(); k++){
  
              Node metaNode = metaList.item(k);
              System.out.println(metaList.getLength());
              rankingDate [k]             = new TextView(this);
              rdate       [k]             = new TextView(this);
              numberOE    [k]             = new TextView(this);
  
              Element metaElement = (Element) metaNode;
              NodeList rankingDateList    = metaElement.getElementsByTagName("date");
              Element rankingDateElement  = (Element) rankingDateList.item(0);
              rankingDateList             = rankingDateElement.getChildNodes();
              rankingDate [k].setText("RankingDate: " + ((Node) rankingDateList.item(0)).getNodeValue());
              layout.addView(rankingDate[k]);
  
              xmlSerializer.startTag(null, "date");
              xmlSerializer.text(((Node) rankingDateList.item(0)).getNodeValue());
              xmlSerializer.endTag(null, "date");
  
          }
  </code></pre>
  
  <p>The system.out.println gives me 1. And k is 0. So why is it a Null Pointer Exception?</p>