java 尝试检索 PackageInfo 时获取空上下文

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27471739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:51:16  来源:igfitidea点击:

Getting Null Context when trying to retrieve PackageInfo

javaandroidandroid-package-managers

提问by Matteo

Spent the entire day on this but can't get it to work.

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

I'm trying to create an app drawer. I want get a list of currently installed applications and then put it in an GridLayout.

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

I'm getting a nullPointerException when I try to call getPackageManagerfrom a class that extends my MainActivity.

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

InstalledApp class:

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

}

}

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

Thanks in advance! :)

提前致谢!:)

回答by michal.luszczuk

You are creating InstalledApp installedApp = new InstalledApp(this);installedApp object which is extending Activity, but this activity is not created by the system but by you. So this InstalledApp object does not have Contextobject bounded to system.

您正在创建InstalledApp installedApp = new InstalledApp(this); 扩展 Activity 的 installedApp 对象,但此 Activity 不是由系统创建的,而是由您创建的。所以这个 InstalledApp 对象没有绑定到系统的Context对象。

Invoke of getPackageManager().getInstalledPackages(0);is possible because MainActivity extending Context, but its null...

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

Better does not extend MainActivity with InstalledApp. And use context. getPackageManager().getInstalledPackages(0);context field which you pass in InstalledApp constructor

Better 不使用 InstalledApp 扩展 MainActivity。并使用 context. getPackageManager().getInstalledPackages(0);您在 InstalledApp 构造函数中传递的上下文字段

回答by CommonsWare

You cannot just create a subclass of Activity, create an instance of it manually, and expect it to work.

您不能只是创建 的子类Activity,手动创建它的实例,然后期望它工作。

Move the getPackages()and getInstalledApps()methods into MainActivity, and you will have better luck.

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