xiaoyan_12 2013-04-22 06:52 采纳率: 25%
浏览 2629
已采纳

listview 运行时的异常

我想在一个 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条回答

  • Baby_Bonnie 2013-04-22 08:21
    关注

    看你的代码有一点小错误,
    public class InstalledApps extends ListActivity
    改成 public class InstalledApps extends Activity
    如果你继承 ListActivity,而后就会获得错误:java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)