尝试检索PackageInfo时获取Null上下文

时间:2014-12-14 17:05:32

标签: java android android-package-managers

花了一整天的时间,但无法让它发挥作用。

我试图创建一个app抽屉。 我想获取当前安装的应用程序列表,然后将其放在GridLayout中。

当我尝试从扩展MainActivity的类中调用getPackageManager时,我收到了nullPointerException。

InstalledApp类:

public class InstalledApp extends MainActivity {
Context context = this;

public InstalledApp(Context c) {
    this.context = c;
}

class PInfo {
    public String appname = "";
    public String pname = "";
    public String versionName = "";
    public int versionCode = 0;
    public Drawable icon;
    public void prettyPrint() {
        System.out.println(appname + "\t" + pname + "\t" + versionName + "\t" + versionCode);
    }
}

public ArrayList<PInfo> getPackages() {
    ArrayList<PInfo> apps = getInstalledApps(false); /* false = no system packages */ //Line 29
    final int max = apps.size();
    for (int i=0; i<max; i++) {
        apps.get(i).prettyPrint();
    }
    return apps;
}

private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
    ArrayList<PInfo> res = new ArrayList<>();
    List<PackageInfo> packs = getPackageManager().getInstalledPackages(0); //line 39
    for( int i = 0; i < packs.size(); i++ ) {
        PackageInfo p = packs.get(i);
        if ((!getSysPackages) && (p.versionName == null)) {
            continue ;
        }
        InstalledApp.PInfo newInfo = new PInfo();
        newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
        newInfo.pname = p.packageName;
        newInfo.versionName = p.versionName;
        newInfo.versionCode = p.versionCode;
        newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
        res.add(newInfo);
    }
    return res;
}

}

在MainActivity中:

InstalledApp installedApp = new InstalledApp(this);
    ArrayList<InstalledApp.PInfo> pInfoArrayList = installedApp.getPackages(); //Line 73
    GridAdapter adapter = new GridAdapter(this, pInfoArrayList);
    gridLayout.setAdapter(adapter);

    gridLayout.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });

Log cat:

     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
        at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:90)
        at squaredem.materiallauncher.InstalledApp.getInstalledApps(InstalledApp.java:39)
        at squaredem.materiallauncher.InstalledApp.getPackages(InstalledApp.java:29)
        at squaredem.materiallauncher.MainActivity.onCreate(MainActivity.java:73)

提前致谢! :)

2 个答案:

答案 0 :(得分:5)

您正在创建正在扩展活动的 InstalledApp installedApp = new InstalledApp(this); installedApp对象,但此活动不是由系统创建的,而是由您创建的。因此,此InstalledApp对象没有将 Context 对象绑定到系统。

调用getPackageManager().getInstalledPackages(0);是可能的,因为MainActivity扩展了Context,但是它为null ...

最好不要使用InstalledApp扩展MainActivity。并使用您在InstalledApp构造函数

中传递的context. getPackageManager().getInstalledPackages(0);上下文字段

答案 1 :(得分:1)

您不能只创建Activity的子类,手动创建它的实例,并希望它能够正常工作。

getPackages()getInstalledApps()方法移至MainActivity,您将获得更好的运气。