xiaoyan_12
2013-04-22 06:52listview 运行时的异常
我想在一个 list view 显示用户安装的 app。我尝试了许多方法,比如 @android:id 和 @+id,但是都不能运行。我的代码哪里出错了呢?
applist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
listrow.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:textSize="16sp"
android:id="@+id/rowTextView">
</TextView>
InstalledApp 类
public class InstalledApps extends ListActivity{
private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.applist);
listview = (ListView)findViewById(R.id.listView1);
PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
List<ApplicationInfo> installedApps = new ArrayList<ApplicationInfo>();
for(ApplicationInfo app : packages) {
//checks for flags; if flagged, check if updated system app
if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1) {
//installedApps.add(app);
//it's a system app, not interested
} else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
//Discard this one
//in this case, it should be a user-installed app
} else {
installedApps.add(app);
}
}
ArrayAdapter<ApplicationInfo> adapter = new ArrayAdapter<ApplicationInfo>(this,
R.layout.listrow, installedApps);
listview.setAdapter(adapter);
}
}
- 点赞
- 回答
- 收藏
- 复制链接分享
3条回答
为你推荐
- Android socket通讯客户端连不上服务器问题,以及发送消息程序就闪退
- socket
- android
- 通讯
- 7个回答
- 关于安卓页面跳转的问题
- android
- 7个回答
- 运行到setContentView这里崩溃了
- 崩溃
- 新闻应用
- android
- 碎片
- 编程
- 5个回答
- 安卓 锁屏/显示或者隐藏虚拟菜单导致侧拉菜单listview的适配报空指针异常
- 运行空指针
- android
- 数据保存及恢复
- 2个回答
- 请大神指教!shareSDK第三方登录,返回了用户登录信息,但是一直报空指针异常,可是有返回值
- android
- sharesdk
- 4个回答
换一换