【问题标题】:Android: Populate ListPreference with installed applications?Android:使用已安装的应用程序填充 ListPreference?
【发布时间】:2011-03-17 00:47:29
【问题描述】:

我对 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。有什么建议么?

【问题讨论】:

    标签: android android-preferences sharedpreferences listpreference preferencescreen


    【解决方案1】:

    我已经尝试过你的代码,它对我来说很好,都像这样以编程方式添加偏好:

      AppSelectorPreference pref2 = new AppSelectorPreference(this, null);
      getPreferenceScreen().addPreference(pref2);
    

    并像你写的那样使用 xml。出现错误的第 169 行是哪一行?

    另外,您是否查看了 logcat 以查看是否有任何应用程序名称或标签给出了类似 null 的内容?我唯一能想到的是,你的安卓应用和我的不一样。

    编辑:对不起,我再次测试,现在我得到了和你一样的错误。不知道我做了什么不同,除了选择一个值

    但是,我通过覆盖 findIndexOfValue 方法来修复它。我这样做只是为了测试:

    @Override
    public int findIndexOfValue(String value) {
        return 0;
        //return super.findIndexOfValue(value);
    }
    

    当然,您需要为该值实现 findind 索引。可能是非常大的条目数组的 android 错误?

    【讨论】:

    • 我希望我知道第 169 行是什么。错误在我的应用程序之外,我相信 ListPreference.java 是操作系统的一部分。我想我应该去挖掘一下,看看那个方法发生了什么......
    【解决方案2】:

    我建议你删除 if 条件,因为它是导致空指针错误的原因,因为条件将是 false 无法执行命令 null 条目一个值

     try {
                int i = 0;
                for (PackageInfo p : appListInfo) {
                        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();
            }   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 2011-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多