title: Getting Null Context when trying to retrieve PackageInfo
body: <p>Spent the entire day on this but can't get it to work.</p>
  
  <p>I'm trying to create an app drawer.
  I want get a list of currently installed applications and then put it in an GridLayout.</p>
  
  <p>I'm getting a nullPointerException when I try to call <code>getPackageManager</code> from a class that extends my MainActivity.</p>
  
  <p>InstalledApp class:</p>
  
  <pre><code>public class InstalledApp extends MainActivity {
  Context context = this;
  
  public InstalledApp(Context c) {
      this.context = c;
  }
  
  class PInfo {
      public String appname = "";
      public String pname = "";
      public String versionName = "";
      public int versionCode = 0;
      public Drawable icon;
      public void prettyPrint() {
          System.out.println(appname + "\t" + pname + "\t" + versionName + "\t" + versionCode);
      }
  }
  
  public ArrayList&lt;PInfo&gt; getPackages() {
      ArrayList&lt;PInfo&gt; apps = getInstalledApps(false); /* false = no system packages */ //Line 29
      final int max = apps.size();
      for (int i=0; i&lt;max; i++) {
          apps.get(i).prettyPrint();
      }
      return apps;
  }
  
  private ArrayList&lt;PInfo&gt; getInstalledApps(boolean getSysPackages) {
      ArrayList&lt;PInfo&gt; res = new ArrayList&lt;&gt;();
      List&lt;PackageInfo&gt; packs = getPackageManager().getInstalledPackages(0); //line 39
      for( int i = 0; i &lt; packs.size(); i++ ) {
          PackageInfo p = packs.get(i);
          if ((!getSysPackages) &amp;&amp; (p.versionName == null)) {
              continue ;
          }
          InstalledApp.PInfo newInfo = new PInfo();
          newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
          newInfo.pname = p.packageName;
          newInfo.versionName = p.versionName;
          newInfo.versionCode = p.versionCode;
          newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
          res.add(newInfo);
      }
      return res;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>In MainActivity:</p>
  
  <pre><code>InstalledApp installedApp = new InstalledApp(this);
      ArrayList&lt;InstalledApp.PInfo&gt; pInfoArrayList = installedApp.getPackages(); //Line 73
      GridAdapter adapter = new GridAdapter(this, pInfoArrayList);
      gridLayout.setAdapter(adapter);
  
      gridLayout.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          public void onItemClick(AdapterView&lt;?&gt; parent, View v, int position, long id) {
              Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
          }
      });
  </code></pre>
  
  <p>Log cat:</p>
  
  <pre><code>     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
          at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:90)
          at squaredem.materiallauncher.InstalledApp.getInstalledApps(InstalledApp.java:39)
          at squaredem.materiallauncher.InstalledApp.getPackages(InstalledApp.java:29)
          at squaredem.materiallauncher.MainActivity.onCreate(MainActivity.java:73)
  </code></pre>
  
  <p>Thanks in advance! :)</p>