问题
my listview contains all app installed.. when i click over one i want open a new activity but actually i get an error.. This is the code:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long row) {
PackageInfo packageInfo = (PackageInfo) parent.getItemAtPosition(position);
AppDataActivity appData = (AppDataActivity) getApplicationContext();
appData.setPackageInfo(packageInfo);
Toast.makeText(MainActivity.this, position, Toast.LENGTH_LONG).show();
Intent appInfo = new Intent(MainActivity.this, ApkInfoActivity.class); startActivity(appInfo);
}
and this is the log:
10-30 14:37:56.780: E/AndroidRuntime(20993): FATAL EXCEPTION: main
10-30 14:37:56.780: E/AndroidRuntime(20993): java.lang.ClassCastException: android.content.pm.ApplicationInfo cannot be cast to android.content.pm.PackageInfo
10-30 14:37:56.780: E/AndroidRuntime(20993): at com.dd.application.MainActivity.onItemClick(MainActivity.java:370)
10-30 14:37:56.780: E/AndroidRuntime(20993): at android.widget.AdapterView.performItemClick(AdapterView.java:297)
10-30 14:37:56.780: E/AndroidRuntime(20993): at android.widget.AbsListView.performItemClick(AbsListView.java:1149)
10-30 14:37:56.780: E/AndroidRuntime(20993): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2939)
10-30 14:37:56.780: E/AndroidRuntime(20993): at android.widget.AbsListView$2.run(AbsListView.java:3622)
10-30 14:37:56.780: E/AndroidRuntime(20993): at android.os.Handler.handleCallback(Handler.java:730)
10-30 14:37:56.780: E/AndroidRuntime(20993): at android.os.Handler.dispatchMessage(Handler.java:92)
10-30 14:37:56.780: E/AndroidRuntime(20993): at android.os.Looper.loop(Looper.java:137)
10-30 14:37:56.780: E/AndroidRuntime(20993): at android.app.ActivityThread.main(ActivityThread.java:5323)
10-30 14:37:56.780: E/AndroidRuntime(20993): at java.lang.reflect.Method.invokeNative(Native Method)
10-30 14:37:56.780: E/AndroidRuntime(20993): at java.lang.reflect.Method.invoke(Method.java:525)
10-30 14:37:56.780: E/AndroidRuntime(20993): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:743)
10-30 14:37:56.780: E/AndroidRuntime(20993): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:559)
10-30 14:37:56.780: E/AndroidRuntime(20993): at dalvik.system.NativeStart.main(Native Method)
i have already implements the OnItemClickListener
in my MainActivity if necessary.. I can't find the problem but if can help i can using a click using ApplicationInfo in this way:
@Override
public void onItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
final ApplicationInfo app = applist.get(position);...
...
...
but if i use PackageInfo
i can't do it.. How can i solve?
EDIT with adapter
public class ApplicationAdapter extends ArrayAdapter<ApplicationInfo> {
private List<ApplicationInfo> appsList = null;
private Context context;
private PackageManager packageManager;
private List<ApplicationInfo> listOfApp;
public ApplicationAdapter(Context context, int textViewResourceId,
List<ApplicationInfo> appsList) {
super(context, textViewResourceId, appsList);
this.context = context;
this.appsList = appsList;
this.listOfApp = new ArrayList<ApplicationInfo>(); //Added here
packageManager = context.getPackageManager();
listOfApp.addAll(appsList);
}
@Override
public int getCount() {
return ((null != appsList) ? appsList.size() : 0);
}
@Override
public ApplicationInfo getItem(int position) {
return ((null != appsList) ? appsList.get(position) : null);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (null == view) {
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.snippet_list_row, null);
}
ApplicationInfo data = appsList.get(position);
if (null != data) {
TextView appName = (TextView) view.findViewById(R.id.app_name);
TextView packageName = (TextView) view.findViewById(R.id.app_paackage);
ImageView iconview = (ImageView) view.findViewById(R.id.app_icon);
appName.setText(data.loadLabel(packageManager));
packageName.setText(data.packageName);
iconview.setImageDrawable(data.loadIcon(packageManager));
}
return view;
}
// Filter Class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
appsList.clear();
if (charText.length() == 0) {
appsList.addAll(listOfApp);
}
else
{
for (ApplicationInfo ai : listOfApp)
{
if (ai.loadLabel(packageManager).toString().toLowerCase(Locale.getDefault()).contains(charText))
{
appsList.add(ai);
}
}
}
notifyDataSetChanged();
}
}
AppData: public class AppDataActivity extends Application {
PackageInfo packageInfo;
public PackageInfo getPackageInfo() {
return packageInfo;
}
public void setPackageInfo(PackageInfo packageInfo) {
this.packageInfo = packageInfo;
}
}
回答1:
You adapter is filled up with a collections of ApplicationInfo
, so the correct cast is to ApplicationInfo
.
change
PackageInfo packageInfo = (PackageInfo) parent.getItemAtPosition(position);
with
ApplicationInfo packageInfo = (ApplicationInfo) parent.getItemAtPosition(position);
来源:https://stackoverflow.com/questions/19684010/cannot-be-cast-to-android-content-pm-packageinfo-on-click-in-my-listview