程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-listview 運行時的異常

android-listview 運行時的異常

編輯:編程綜合問答
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);
    }
}

最佳回答:


看你的代碼有一點小錯誤,
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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved