开发者

Android: Populate ListPreference with installed applications?

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.

I have created the following class:

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);
}

}

This is added to my PreferenceScreen with the following XML:

        <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>

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?


I have tried your code, and it works fine for me, both adding the prefence programatically like this:

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

and using xml like you wrote. Which one is the line 169 where the error is appearing?

Also, did you look through logcat to see if any of the app names or labels is giving something like null? The only thing I can think is that your android has different apps from mine.

Edit: Sorry, I tested again and now I got the same error as you. Not sure what I did different, other than select a value

However, I fixed it by overriding the findIndexOfValue method. I just did this to test:

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

but of course you will need to implement findind the index for that value. Might be an android bug for very large entries arrays?


i would suggest you to remove the if condition, cause it is what causes a null pointer error since the condition will be false unable to execute the commands null entries a value

 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();
        }   
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜