Android:使用已安装的应用程序填充ListPreference?

问题描述

| 我对android开发有些陌生,这是我第一次尝试在首选项屏幕中提供已安装应用程序的列表。从此列表中选择的值将稍后用于启动用户选择的应用。 我创建了以下课程:
public class AppSelectorPreference extends ListPreference {

public AppSelectorPreference(Context context,AttributeSet attrs) {
    super(context,attrs);

    PackageManager pm = context.getPackageManager();
    List<PackageInfo> 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 > 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> Put descriptive error message here\");
        e.printStackTrace();
    }   

    setEntries(entries);
    setEntryValues(entryValues);
}

}
使用以下XML将其添加到我的PreferenceScreen中:
        <PreferenceCategory android:title=\"Launch Application\">
        <CheckBoxPreference
            android:title=\"Launch Application\"
            android:summary=\"Launch an application\"
            android:defaultValue=\"false\"
            android:key=\"pref_connect_launch_enable\"
            android:dependency=\"pref_connect_enable\"/>
        <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\"/>
    </PreferenceCategory>
看来一切正常,直到单击AppSelectorPreference为止,这会在android.preference.ListPreference.findIndexOfValue(ListPreference.java:169)中引发NPE。有什么建议么?     

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)