gpt4 book ai didi

android - java.lang.classcastException android.content.pm.ResolveInfo

转载 作者:行者123 更新时间:2023-11-29 22:09:15 25 4
gpt4 key购买 nike

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

应用程序和图标显示正常,但是当我单击列表项时出现以下错误。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}但我只想买相机。这样我的点击代码就可以工作了。我应该怎么做才能解决这个错误。谢谢。

最佳答案

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

试试这个

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

让我知道发生了什么..

关于android - java.lang.classcastException android.content.pm.ResolveInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10052279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com