簡體   English   中英

Android:使用已安裝的應用程序填充ListPreference?

[英]Android: Populate ListPreference with installed applications?

我對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。 有什么建議么?

我建議您刪除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();
        }   

我已經嘗試了您的代碼,並且對我來說效果很好,都以編程方式添加了如下代碼:

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

並像您寫的那樣使用xml。 出現錯誤的行169是哪一個?

另外,您是否瀏覽了logcat以查看是否有任何應用程序名稱或標簽給出了null之類的信息? 我唯一能想到的是您的android有與我不同的應用程序。

編輯:對不起,我再次測試,現在我得到了與您相同的錯誤。 除了選擇一個值外,不知道我做了什么不同

但是,我通過重寫findIndexOfValue方法來解決此問題。 我只是這樣做來測試:

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

但是當然您需要實現該值的findind索引。 可能是很大條目數組的android bug嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM