gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:00:09 27 4
gpt4 key购买 nike

为此花了一整天,但无法正常工作。

我正在尝试创建一个应用程序抽屉。我想获取当前已安装应用程序的列表,然后将其放入 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;
}

在主 Activity 中:

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

日志猫:

     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)

提前致谢! :)

最佳答案

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

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

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

关于java - 尝试检索 PackageInfo 时获取空上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27471739/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com