问题描述
我的列表视图中包含安装所有应用..当我点击了一个我想打开一个新的活动,但实际上我得到一个错误。这是code:
@覆盖
公共无效onItemClick(适配器视图<>母公司,观景,INT位,排着长长){
PackageInfo packageInfo =(PackageInfo)parent.getItemAtPosition(位置);
AppDataActivity APPDATA =(AppDataActivity)getApplicationContext();
appData.setPackageInfo(packageInfo);
Toast.makeText(MainActivity.this,位置,Toast.LENGTH_LONG).show();
意图APPINFO =新意图(MainActivity.this,ApkInfoActivity.class); startActivity(APPINFO); }
和这是日志:
14 10-30:37:56.780:E / AndroidRuntime(20993):致命异常:主要
10-30 14:37:56.780:E / AndroidRuntime(20993):java.lang.ClassCastException:android.content.pm.ApplicationInfo不能转换为android.content.pm.PackageInfo
10-30 14:37:56.780:E / AndroidRuntime(20993):在com.dd.application.MainActivity.onItemClick(MainActivity.java:370)
10-30 14:37:56.780:E / AndroidRuntime(20993):在android.widget.AdapterView.performItemClick(AdapterView.java:297)
10-30 14:37:56.780:E / AndroidRuntime(20993):在android.widget.AbsListView.performItemClick(AbsListView.java:1149)
10-30 14:37:56.780:E / AndroidRuntime(20993):在android.widget.AbsListView $ PerformClick.run(AbsListView.java:2939)
10-30 14:37:56.780:E / AndroidRuntime(20993):在android.widget.AbsListView $ 2.run(AbsListView.java:3622)
10-30 14:37:56.780:E / AndroidRuntime(20993):在android.os.Handler.handleCallback(Handler.java:730)
10-30 14:37:56.780:E / AndroidRuntime(20993):在android.os.Handler.dispatchMessage(Handler.java:92)
10-30 14:37:56.780:E / AndroidRuntime(20993):在android.os.Looper.loop(Looper.java:137)
10-30 14:37:56.780:E / AndroidRuntime(20993):在android.app.ActivityThread.main(ActivityThread.java:5323)
10-30 14:37:56.780:E / AndroidRuntime(20993):在java.lang.reflect.Method.invokeNative(本机方法)
10-30 14:37:56.780:E / AndroidRuntime(20993):在java.lang.reflect.Method.invoke(Method.java:525)
10-30 14:37:56.780:E / AndroidRuntime(20993):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:743)
10-30 14:37:56.780:E / AndroidRuntime(20993):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:559)
10-30 14:37:56.780:E / AndroidRuntime(20993):在dalvik.system.NativeStart.main(本机方法)
我已经实现了 OnItemClickListener
在我MainActivity如果需要的话。我无法找到问题,但如果能帮助我可以用这种方式使用ApplicationInfo点击
@覆盖
公共无效onItemClick(ListView中升,视图V,INT位置,长的id){
super.onListItemClick(L,V,位置ID); 最后ApplicationInfo应用= applist.get(位置); ...
...
...
但如果我使用 PackageInfo
我不能做到这一点..我该如何解决?
编辑与适配器
公共类ApplicationAdapter扩展ArrayAdapter< ApplicationInfo> {
私人列表< ApplicationInfo> appsList = NULL;
私人上下文的背景下;
私人软件包管理系统软件包管理系统;
私人列表< ApplicationInfo> listOfApp; 公共ApplicationAdapter(上下文的背景下,INT textViewResourceId,
清单< ApplicationInfo> appsList){
超(背景下,textViewResourceId,appsList);
this.context =背景;
this.appsList = appsList;
this.listOfApp =新的ArrayList< ApplicationInfo>(); //此处添加
软件包管理系统= context.getPackageManager();
listOfApp.addAll(appsList); } @覆盖
公众诠释的getCount(){
收益率((空= appsList)appsList.size():?0);
} @覆盖
公共ApplicationInfo的getItem(INT位置){
收益率((空= appsList)appsList.get(位置):!NULL);
} @覆盖
众长getItemId(INT位置){
返回的位置;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
查看查看= convertView;
如果(空==视图){
LayoutInflater layoutInflater =(LayoutInflater)上下文
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
鉴于= layoutInflater.inflate(R.layout.snippet_list_row,NULL);
} ApplicationInfo数据= appsList.get(位置);
如果(空!=数据){
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(软件包管理系统));
packageName.setText(data.packageName);
iconview.setImageDrawable(data.loadIcon(软件包管理系统));
}
返回视图。
} //过滤等级
公共无效过滤器(字符串charText){
charText = charText.toLowerCase(Locale.getDefault());
appsList.clear();
如果(charText.length()== 0){
appsList.addAll(listOfApp);
}
其他
{
对于(ApplicationInfo AI:listOfApp)
{
如果(ai.loadLabel(软件包管理系统)的ToString()。与toLowerCase(Locale.getDefault())。包括(charText))
{
appsList.add(AI);
}
}
}
notifyDataSetChanged();
}}
应用程序数据:
公共类AppDataActivity扩展应用{
PackageInfo packageInfo; 公共PackageInfo getPackageInfo(){
返回packageInfo;
} 公共无效setPackageInfo(PackageInfo packageInfo){
this.packageInfo = packageInfo;
}
}
您适配器充满了 ApplicationInfo
的集合,所以正确的投是 ApplicationInfo
。
变更
PackageInfo packageInfo =(PackageInfo)parent.getItemAtPosition(位置);
与
ApplicationInfo packageInfo =(ApplicationInfo)parent.getItemAtPosition(位置);
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;
}
}
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);
这篇关于不能在我的列表视图被转换为android.content.pm.PackageInfo上点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!