我正在尝试获取设备上已安装应用程序的列表,然后在列表中显示其名称 但是,当我运行我的代码时,它显示不幸的是,Lister已被停止 我在onCreate方法中的代码是: -
PackageManager pm=this.getPackageManager();
List<ApplicationInfo> list=pm.getInstalledApplications(0);
ListView lv=(ListView)findViewById(R.id.listView1);
ArrayList<String> al=new ArrayList<String>();
for(ApplicationInfo app:list)
{
String appN=pm.getApplicationLabel(app).toString();
al.add(appN);
}
ArrayAdapter<String> arr=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,android.R.id.text1,al);
lv.setAdapter(arr);
setContentView(R.layout.activity_sec);
和我的 activity_sec.xml 代码是: -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.gestureview.Sec" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="40dp" >
</ListView>
</RelativeLayout>
我的Log Cat是: -
06-19 04:33:53.234: E/AndroidRuntime(1205): FATAL EXCEPTION: main
06-19 04:33:53.234: E/AndroidRuntime(1205): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gestureview/com.example.gestureview.Sec}: java.lang.NullPointerException
06-19 04:33:53.234: E/AndroidRuntime(1205): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-19 04:33:53.234: E/AndroidRuntime(1205): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-19 04:33:53.234: E/AndroidRuntime(1205): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-19 04:33:53.234: E/AndroidRuntime(1205): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-19 04:33:53.234: E/AndroidRuntime(1205): at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 04:33:53.234: E/AndroidRuntime(1205): at android.os.Looper.loop(Looper.java:137)
06-19 04:33:53.234: E/AndroidRuntime(1205): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-19 04:33:53.234: E/AndroidRuntime(1205): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 04:33:53.234: E/AndroidRuntime(1205): at java.lang.reflect.Method.invoke(Method.java:511)
06-19 04:33:53.234: E/AndroidRuntime(1205): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-19 04:33:53.234: E/AndroidRuntime(1205): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-19 04:33:53.234: E/AndroidRuntime(1205): at dalvik.system.NativeStart.main(Native Method)
06-19 04:33:53.234: E/AndroidRuntime(1205): Caused by: java.lang.NullPointerException
06-19 04:33:53.234: E/AndroidRuntime(1205): at com.example.gestureview.Sec.onCreate(Sec.java:33)
06-19 04:33:53.234: E/AndroidRuntime(1205): at android.app.Activity.performCreate(Activity.java:5104)
06-19 04:33:53.234: E/AndroidRuntime(1205): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-19 04:33:53.234: E/AndroidRuntime(1205): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-19 04:33:53.234: E/AndroidRuntime(1205): ... 11 more
创建ListView似乎一定存在问题。
答案 0 :(得分:2)
首先应该设置内容而不是调用ListView
PackageManager pm=this.getPackageManager();
List<ApplicationInfo> list=pm.getInstalledApplications(0);
setContentView(R.layout.activity_sec); ///call this Line here
ListView lv=(ListView)findViewById(R.id.listView1);
ArrayList<String> al=new ArrayList<String>();
for(ApplicationInfo app:list)
{
String appN=pm.getApplicationLabel(app).toString();
al.add(appN);
}
ArrayAdapter<String> arr=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,android.R.id.text1,al);
lv.setAdapter(arr);
答案 1 :(得分:0)
这也很有效
final PackageManager pm = mParentActivity.getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(0);
for (ApplicationInfo packageInfo : packages) {
// get the UID for the selected app
int UID = packageInfo.uid;
String package_name = packageInfo.packageName;
ApplicationInfo app = null;
try {
app = pm.getApplicationInfo(package_name, 0);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String name = (String) pm.getApplicationLabel(app);
Drawable icon = pm.getApplicationIcon(app);
final SmsDetailsPojo dataItem = new SmsDetailsPojo();
dataItem.setContactNum(name);
dataItem.setDrawable(icon);
if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1) {
// updated system apps
} else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
// system apps
} else {
arrayUserInstalledApps.add(dataItem);
}