title: Android: Populate ListPreference with installed applications?
body: <p>I am somewhat new to android development, this is my first time trying to provide a list of installed applications in a preference screen.  The selected value from this list will later be used to launch an app of the user's choosing.</p>
  
  <p>I have created the following class:</p>
  
  <pre><code>public class AppSelectorPreference extends ListPreference {
  
  public AppSelectorPreference(Context context, AttributeSet attrs) {
      super(context,attrs);
  
      PackageManager pm = context.getPackageManager();
      List&lt;PackageInfo&gt; appListInfo = pm.getInstalledPackages(0); 
      CharSequence[] entries = new CharSequence[appListInfo.size()];
      CharSequence[] entryValues = new CharSequence[appListInfo.size()];
  
      try {
          int i = 0;
          for (PackageInfo p : appListInfo) {
              if (p.applicationInfo.uid &gt; 10000) {
                  entries[i] = p.applicationInfo.loadLabel(pm).toString();
                  entryValues[i] = p.applicationInfo.packageName.toString();              
                  Log.d(BT,"Label: " + entries[i]);
                  Log.d(BT,"PName: " + entryValues[i]);
                  i++;
              }         
          }
      } catch (Exception e) {
          Log.e(BT,"ER&gt; Put descriptive error message here");
          e.printStackTrace();
      }   
  
      setEntries(entries);
      setEntryValues(entryValues);
  }
  
  }
  </code></pre>
  
  <p>This is added to my PreferenceScreen with the following XML:</p>
  
  <pre><code>        &lt;PreferenceCategory android:title="Launch Application"&gt;
          &lt;CheckBoxPreference
              android:title="Launch Application"
              android:summary="Launch an application"
              android:defaultValue="false"
              android:key="pref_connect_launch_enable"
              android:dependency="pref_connect_enable"/&gt;
          &lt;com.nirsoftware.AppSelectorPreference
              android:title="Select Application"
              android:summary="Select application to launch"
              android:key="pref_connect_package_name"
              android:dependency="pref_connect_launch_enable"/&gt;
      &lt;/PreferenceCategory&gt;
  </code></pre>
  
  <p>It appears everything is working correctly, until clicking on the AppSelectorPreference, which throws an NPE in  android.preference.ListPreference.findIndexOfValue(ListPreference.java:169).  Any suggestions? </p>