1

我正在尝试显示所有已安装的应用程序及其图标。单击列表项后,我将显示该应用程序的相关记录。

应用程序和图标显示正确,但是当我单击列表项时出现以下错误。java.lang.classcastException android.content.pm.ResolveInfo

final PackageManager pm = this.getPackageManager();

    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    final ArrayList<ResolveInfo> list = (ArrayList<ResolveInfo>) pm
            .queryIntentActivities(intent,
                    PackageManager.PERMISSION_GRANTED);
    for (ResolveInfo rInfo : list) {
        Log.i("taa", ": Installed Applications "
                + rInfo.activityInfo.applicationInfo.loadLabel(pm)
                        .toString());
    }

    final ArrayAdapter<ResolveInfo> adapter = new ArrayAdapter<ResolveInfo>(
            this, R.layout.installedapp, list) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null)
                convertView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.installedapp, parent, false);

            final String text = list.get(position).activityInfo.applicationInfo
                    .loadLabel(pm).toString();
            ((TextView) convertView.findViewById(R.id.text)).setText(text);

            final Drawable drawable = list.get(position).activityInfo.applicationInfo
                    .loadIcon(pm);
            ((ImageView) convertView.findViewById(R.id.image))
                    .setImageDrawable(drawable);

            return convertView;
        }

    };
    setListAdapter(adapter);

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

}

protected void onListItemClick(ListView l, View v, int position, long id) {
    String item = (String) getListAdapter().getItem(position);///////---->error
    List<String> app_item = dh.getRecordItem(item);
    // Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
    Intent intent = new Intent(ApplicationList.this, AppContent.class);
    Bundle bundle = new Bundle();

    if (app_item.get(0).equals("No records")) {
        Log.d("Set", "No Activity happened");
        bundle.putString("list", app_item.toString());
    } else {
        Log.d("Set", "Act happened");
        bundle.putString("list", app_item.toString());
    }
    intent.putExtras(bundle);
    startActivity(intent);

}

我试图在日志中打印项目的值我得到了这个值resolverInfo{405437c0 com.android.camera gallaru pickerp=0 o=0 m=0x108000} 但我不想只得到相机。这样我的点击代码就可以工作了。我应该怎么做才能解决这个错误。谢谢。

4

1 回答 1

2

实际上,您正在将ResolveInfo对象转换为String对象,这就是您得到异常的原因..

尝试这个

protected void onListItemClick(ListView l, View v, int position, long id) {
 ResolveInfo info = (ResolveInfo) getListAdapter().getItem(position);
 String item =  info.activityInfo.name;
.
.
.

让我知道发生了什么..

于 2012-04-07T06:27:32.223 回答