user340
user340

Reputation: 383

listview giving runtime exception

I want to list the user installed apps in a list view. I had tried many approaches, such as @android:id and @+id but nothing seems to work..can some one point out what is the error in this code..

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 class

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);
    }
}

Upvotes: 0

Views: 299

Answers (2)

Raghav
Raghav

Reputation: 4638

Your code seems to be alright only a minor mistake try public class InstalledApps extends Activity instead of public class InstalledApps extends ListActivity if you extend ListActivity then you will get error Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

Upvotes: 1

oldnew
oldnew

Reputation: 11

ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code)

Upvotes: 1

Related Questions