簡體   English   中英

嘗試檢索PackageInfo時獲取空上下文

[英]Getting Null Context when trying to retrieve PackageInfo

花了整整一整天的時間,但無法正常工作。

我正在嘗試創建一個應用程序抽屜。 我想要獲取當前安裝的應用程序列表,然后將其放入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();
        }
    });

日志貓:

     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)

提前致謝! :)

您正在創建InstalledApp installedApp = new InstalledApp(this); installedApp對象,它擴展了Activity,但此活動不是由系統而是由您創建的。 因此,此InstalledApp對象沒有與系統綁定的Context對象。

調用getPackageManager().getInstalledPackages(0); 可能是因為MainActivity擴展了Context,但是它的null ...

Better不會通過InstalledApp擴展MainActivity。 並使用context. getPackageManager().getInstalledPackages(0); context. getPackageManager().getInstalledPackages(0); 您在InstalledApp構造函數中傳遞的上下文字段

您不能只創建Activity的子類,手動創建它的實例,並期望它能工作。

getPackages()getInstalledApps()方法移至MainActivity ,您會獲得更好的運氣。

暫無
暫無

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

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