如何在android中获取已安装的应用程序名称?
我做了一个小方法来显示android中已安装的应用程序名称。但是当我给出“name”属性时,它显示异常错误。当我给出“packageName”时,该方法完美执行并在列表中显示包名称在
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()
程序抛出异常错误。请帮我解决这个问题。
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);
}
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测是您的代码抛出 NullPointerException 因为
name
字段为空。无论如何,您可能想要的是:My guess is your code is throwing a NullPointerException because the
name
field is null. In any event, what you probably want is:通过使用它,您可以获得已安装的应用程序包名称和应用程序名称
by using this you can get installed app package names and app names