我无法将活动扩展到listactivity

时间:2013-05-15 12:01:48

标签: android android-intent

public class MainActivity extends ListActivity implements OnItemClickListener {
// private ListView lView;
 private ArrayList results = new ArrayList();
 PackageInfo pInfo;
 String packageName;
 String app;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
//  lView= findViewById(R.id.list);
    PackageManager pm = this.getPackageManager();
    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    List < ResolveInfo > list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
    for (ResolveInfo rInfo: list) {
        results.add(rInfo.activityInfo.applicationInfo
            .loadLabel(pm).toString());
        Log.w("Installed Applications", rInfo.activityInfo.applicationInfo
            .loadLabel(pm).toString());
    }
            getListView().setOnItemClickListener(this);
            setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, results));
}


 @Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    pInfo= (PackageInfo) results.get(arg2);
    app= (String) results.get(arg2);
    packageName = pInfo .packageName;
    try {
        Class myapp= Class.forName(packageName + app);
        Intent myintent= new Intent(MainActivity.this, myapp);
        startActivity(myintent);
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

这是我最近的代码。但我一次又一次地得到同样的错误。

05-15 17:19:52.383: E/AndroidRuntime(6370): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ku.installedapps/com.ku.installedapps.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

帮助需要。 M得到与以前相同的错误。 :(

1 个答案:

答案 0 :(得分:1)

在您的XML中,确保您拥有具有Android ID的ListView,如下所示:

而不是你的XML: <ListView android:id="@+id/list",使用此:

<ListView android:id="@android:id/list" ...>