gpt4 book ai didi

android - 已安装应用列表强制关闭 : java. lang.ClassCastException

转载 作者:太空狗 更新时间:2023-10-29 13:30:45 26 4
gpt4 key购买 nike

我正在尝试制作一个提供已安装应用程序列表的应用程序,当单击某个项目时,将启动另一个 Activity ,该 Activity 说明已安装应用程序所需的权限。我得到了已安装的应用程序列表,但是当我单击任何应用程序而不是启动新 Activity 时,应用程序强制关闭。

主要 Activity

package com.example.appslist;

import java.util.List;
import com.example.appslist.adapter.ApkAdapter;
import com.example.appslist.app.AppData;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;


public class ApkListActivity extends Activity implements OnItemClickListener {

PackageManager packageManager;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

packageManager = getPackageManager();
List<PackageInfo> packageList = packageManager
.getInstalledPackages(PackageManager.GET_PERMISSIONS);

ListView mylistview= (ListView) findViewById(android.R.id.list);
mylistview.setAdapter(new ApkAdapter(this, packageList, packageManager));



mylistview.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long row) {
PackageInfo packageInfo = (PackageInfo) parent
.getItemAtPosition(position);
AppData appData = (AppData) getApplicationContext();
appData.setPackageInfo(packageInfo);

Intent appInfo = new Intent(getApplicationContext(), ApkInfo.class);
startActivity(appInfo);
}
}

日志猫

04-02 11:16:04.536: E/asset(2777): MAS: getAppPckgAndVerCode package: com.example.appslist === version 1
04-02 11:16:04.559: D/dalvikvm(2777): Trying to load lib lib_glossary.so 0x0
04-02 11:16:04.559: I/dalvikvm(2777): Unable to dlopen(lib_glossary.so): Cannot load library: load_library[1083]: Library 'lib_glossary.so' not found
04-02 11:16:04.567: E/MBGlossaryManager(2777): lib_glossary.so can't be loaded
04-02 11:16:04.700: E/asset(2777): MAS: getAppPckgAndVerCode package: com.google.android.location === version 8
04-02 11:16:04.708: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.launcher === version 8
04-02 11:16:04.708: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.defcontainer === version 8
04-02 11:16:04.708: E/asset(2777): MAS: getAppPckgAndVerCode package: com.navngo.igo.javaclient === version 148360
04-02 11:16:04.715: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.contacts === version 8
04-02 11:16:04.723: E/asset(2777): MAS: getAppPckgAndVerCode package: com.arcsoft.photoworkshop === version 5
04-02 11:16:04.778: D/dalvikvm(2777): GC_EXTERNAL_ALLOC freed 822 objects / 60704 bytes in 56ms
04-02 11:16:04.786: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.phone === version 8
04-02 11:16:04.794: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.calculator2 === version 8
04-02 11:16:04.794: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.htmlviewer === version 8
04-02 11:16:04.801: E/asset(2777): MAS: getAppPckgAndVerCode package: com.example.myfirstapp === version 1
04-02 11:16:04.801: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.providers.calendar === version 8
04-02 11:16:04.809: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.bluetooth === version 8
04-02 11:16:04.809: E/asset(2777): MAS: getAppPckgAndVerCode package: com.example.sessionmanagement === version 1
04-02 11:16:04.817: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.calendar === version 8
04-02 11:16:04.817: E/asset(2777): MAS: getAppPckgAndVerCode package: com.google.android.location === version 8
04-02 11:16:04.817: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.launcher === version 8
04-02 11:16:04.825: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.defcontainer === version 8
04-02 11:16:04.825: E/asset(2777): MAS: getAppPckgAndVerCode package: com.navngo.igo.javaclient === version 148360
04-02 11:16:04.872: E/asset(2777): MAS: getAppPckgAndVerCode package: com.android.contacts === version 8
04-02 11:16:06.536: D/AndroidRuntime(2777): Shutting down VM
04-02 11:16:06.536: W/dalvikvm(2777): threadid=1: thread exiting with uncaught exception (group=0x400207e0)
04-02 11:16:06.551: E/AndroidRuntime(2777): FATAL EXCEPTION: main
04-02 11:16:06.551: E/AndroidRuntime(2777): java.lang.ClassCastException: android.app.Application
04-02 11:16:06.551: E/AndroidRuntime(2777): at com.example.appslist.ApkListActivity.onItemClick(ApkListActivity.java:44)
04-02 11:16:06.551: E/AndroidRuntime(2777): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
04-02 11:16:06.551: E/AndroidRuntime(2777): at android.widget.ListView.performItemClick(ListView.java:3444)
04-02 11:16:06.551: E/AndroidRuntime(2777): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
04-02 11:16:06.551: E/AndroidRuntime(2777): at android.os.Handler.handleCallback(Handler.java:587)
04-02 11:16:06.551: E/AndroidRuntime(2777): at android.os.Handler.dispatchMessage(Handler.java:92)
04-02 11:16:06.551: E/AndroidRuntime(2777): at android.os.Looper.loop(Looper.java:143)
04-02 11:16:06.551: E/AndroidRuntime(2777): at android.app.ActivityThread.main(ActivityThread.java:4717)
04-02 11:16:06.551: E/AndroidRuntime(2777): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 11:16:06.551: E/AndroidRuntime(2777): at java.lang.reflect.Method.invoke(Method.java:521)
04-02 11:16:06.551: E/AndroidRuntime(2777): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-02 11:16:06.551: E/AndroidRuntime(2777): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-02 11:16:06.551: E/AndroidRuntime(2777): at dalvik.system.NativeStart.main(Native Method)
04-02 11:16:08.137: I/Process(2777): Sending signal. PID: 2777 SIG: 9

我无法理解强制关闭的原因。请帮忙

应用数据.java

package com.example.appslist.app;

import android.app.Application;
import android.content.pm.PackageInfo;

public class AppData extends Application {

PackageInfo packageInfo;

public PackageInfo getPackageInfo() {
return packageInfo;
}

public void setPackageInfo(PackageInfo packageInfo) {
this.packageInfo = packageInfo;
}
}

ApkAdapter.java

package com.example.appslist.adapter;

import java.util.List;
import com.example.appslist.R;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ApkAdapter extends BaseAdapter {
List<PackageInfo> packageList;
Activity context;
PackageManager packageManager;

public ApkAdapter(Activity context, List<PackageInfo> packageList,
PackageManager packageManager) {
super();
this.context = context;
this.packageList = packageList;
this.packageManager = packageManager;
}

private class ViewHolder {
TextView apkName;
}

public int getCount() {
return packageList.size();
}

public Object getItem(int position) {
return packageList.get(position);
}

public long getItemId(int position) {
return 0;
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();

if (convertView == null) {
convertView = inflater.inflate(R.layout.apklist_item, null);
holder = new ViewHolder();

holder.apkName = (TextView) convertView.findViewById(R.id.appname);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

PackageInfo packageInfo = (PackageInfo) getItem(position);
Drawable appIcon = packageManager
.getApplicationIcon(packageInfo.applicationInfo);
String appName = packageManager.getApplicationLabel(
packageInfo.applicationInfo).toString();
appIcon.setBounds(0, 0, 40, 40);
holder.apkName.setCompoundDrawables(appIcon, null, null, null);
holder.apkName.setCompoundDrawablePadding(15);
holder.apkName.setText(appName);

return convertView;
}

}

list 文件

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appslist"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>

<activity
android:name="com.example.appslist.ApkListActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ApkInfo"
android:label="@string/title_activity_apk_info" >
</activity>
</application>

最佳答案

编辑:

It seems you have not registered AppData in the Manifest

尝试添加

  android:name=".AppData"

在 list 的应用程序标签中。

即这个

 <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name=".AppData"
>

错误在这一行

 AppData appData = (AppData) getApplicationContext();

将其更改为

AppData appData = (AppData) getApplication();

关于android - 已安装应用列表强制关闭 : java. lang.ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15757590/

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