2 xiaoyan 12 xiaoyan_12 于 2013.04.22 14:52 提问

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
Baby_Bonnie   2013.04.22 16: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

beiminglei
beiminglei   2013.04.23 14:58

public class GTBehaviorActivity extends ListActivity{

// private List<String> data = new ArrayList<String>();
private List<Map<String, Object>> mData;
private PackageManager mPM;
Intent intent = new  Intent();
private Launcher mlauncher;
private static final String MY_ACTION="com.chenchen.broadcast.action.MY_ACTION";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mData = getData();
    SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.gt_behavior,
            new String[]{"title","img"},
            new int[]{R.id.gt_behavior_title,R.id.gt_behavior_icon});
    setListAdapter(adapter);


}

如果继承listactivity就已经包含默认的listview了,可以直接设置数据显示,如果继承activity的话就需要set一个布局,而且布局中的list的id必须是@android:id/list。

curie_87
curie_87   2013.04.22 17:44

ListActivity 有一个默认的布局在屏幕中间包含一个全屏的 list。但是,你可以使用 setContentView() in onCreate() 设置布局自定义屏幕布局。自定义的视图必须要包含一个 id 是 "@android:id/list"的 ListView 对象。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!