title: Android Oncreate load a listview in AsyncTask and show loader
body: <p>I have a Custom List View which is loaded in Oncreate of my activity but in Async Task. I want to display the activity screen initially with a loader while the list is populated. </p>
  
  <p>Activity OnCreate</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_word);
  
      viewFlipper = new ViewFlipper(this);
      viewFlipper.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
  
  
  
      TextView txt = (TextView) findViewById(R.id.phraseListHeading);
      Typeface font = Typeface.createFromAsset(WordActivity.this.getAssets(), "fonts/NotoSans-Regular.ttf");
  
      String categoryName = getIntent().getExtras().getString("categoryName").toUpperCase();
      category_obj = (categories) getIntent().getSerializableExtra("category_obj");
  
  
  
  
      asyncLoadWordList task2 = new asyncLoadWordList(getApplicationContext());
      task2.execute();}
  </code></pre>
  
  <p>My Async Task Class</p>
  
  <pre><code>public class asyncLoadWordList extends  AsyncTask&lt;ArrayList&lt;word&gt;, Void, ArrayList&lt;word&gt;&gt;{
  
      private Context mContext;
  
      public asyncLoadWordList(Context context) {
          mContext = context;
      }
  
  
      protected void onPreExecute() {
          //Start the splash screen dialog
          if (pleaseWaitDialog == null)
              pleaseWaitDialog= ProgressDialog.show(WordActivity.this,
                      "PLEASE WAIT",
                      "Getting results...",
                      false);
      }
  
      @Override
      protected ArrayList&lt;word&gt; doInBackground(ArrayList&lt;word&gt;... params) {
  
          wordDb = new wordDB(mContext);
          category_obj = (categories) getIntent().getSerializableExtra("category_obj");
          ArrayList&lt;word&gt; words = new ArrayList&lt;word&gt;();
          Cursor row =  wordDb.selectWordList(category_obj.getCsvCategoryId().toString());
          words.add(new word("-1", "-1", "", "", "", "", "", "", "x.mp3", ""));
          words.add(new word("-2", "-2", "", "", "", "", "", "", "x.mp3", ""));
          row.moveToFirst();
          while (!row.isAfterLast()) {
              //Log.d("Data id: ", row.getString(2));
              words.add( new word(row.getString(0),row.getString(1),row.getString(2),row.getString(3),row.getString(4),row.getString(5),row.getString(6),row.getString(7),row.getString(8),row.getString(9)));
              row.moveToNext();
          }
          row.close();
  
          return words;
      }
      protected void onPostExecute(ArrayList&lt;word&gt; result) {
  
          adapter = new WordAdapter(WordActivity.this, result);
  
          ListView listView = (ListView) findViewById(R.id.list_view_word);
          listView.setAdapter(adapter);
  
          if (pleaseWaitDialog != null) {
              pleaseWaitDialog.dismiss();
              pleaseWaitDialog = null;
  
          }
  
          asyncFlipperView task = new asyncFlipperView(getApplicationContext());
          task.execute(new String[]{category_obj.getCsvCategoryId().toString()});
      }
  }
  </code></pre>
  
  <p>I am getting </p>
  
  <pre><code>04-04 14:10:41.889  31208-31208/thai.phrasi.ctech.com.phrasi E/AndroidRuntime﹕ FATAL EXCEPTION: main
  Process: thai.phrasi.ctech.com.phrasi, PID: 31208
  java.lang.RuntimeException: Unable to start activity ComponentInfo{thai.phrasi.ctech.com.phrasi/thai.phrasi.ctech.com.phrasi.WordActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@42169328 is not valid; is your activity running?
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
          at android.app.ActivityThread.startActivityNow(ActivityThread.java:2024)
          at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
          at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
          at thai.phrasi.ctech.com.phrasi.CategoryActivity$1.onItemClick(CategoryActivity.java:95)
          at android.widget.AdapterView.performItemClick(AdapterView.java:299)
          at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
          at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911)
          at android.widget.AbsListView$3.run(AbsListView.java:3645)
          at android.os.Handler.handleCallback(Handler.java:733)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:136)
          at android.app.ActivityThread.main(ActivityThread.java:5001)
          at java.lang.reflect.Method.invokeNative(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:515)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
          at dalvik.system.NativeStart.main(Native Method)
   Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@42169328 is not valid; is your activity running?
          at android.view.ViewRootImpl.setView(ViewRootImpl.java:532)
          at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
          at 
  </code></pre>
  
  <p><strong>How do I show the initial Screen with loader???</strong></p>