如何在Android中获取已安装的应用程序名称? [英] How to get installed application name in android?
问题描述
我做了一个小方法来在android中显示已安装的应用程序名称.但是当我给名称"属性显示异常错误.当我输入"packageName"时,该方法会完美执行并在列表中显示软件包名称
I have made a small method to display the installed application name in android. But when i give "name" attribute its showing exception error. And when i give "packageName" the method executes perfectly and displays the package name in a list
private void getInstalledApps() {
// TODO Auto-generated method stub
PackageManager packageManager=this.getPackageManager();
List<ApplicationInfo applist=packageManager.getInstalledApplications(0);
Iterator<ApplicationInfo> it=applist.iterator();
while(it.hasNext()){
ApplicationInfo pk=(ApplicationInfo)it.next();
String appname=pk.name.toString();
installedapplist.add(appname);
}
}
在上面的代码中,当我给出String appname=pk.packageName.toString()
时,它工作正常,但是当我给出String appname=pk.name.toString()
时,程序将引发异常错误.请帮助我解决问题.
In the above code when i give String appname=pk.packageName.toString()
it works fine but when I give String appname=pk.name.toString()
the program is throwing an exception error. Please help me to sort out the problem.
推荐答案
我的猜测是您的代码正在抛出NullPointerException,因为name
字段为null.无论如何,您可能想要的是:
My guess is your code is throwing a NullPointerException because the name
field is null. In any event, what you probably want is:
String appname = packageManager.getApplicationLabel(pk).toString()
这篇关于如何在Android中获取已安装的应用程序名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!