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 得到与以前相同的错误。:(