在 Android 中将应用程序转换为 AppData 时如何解决 Class 转换异常

How to resolve Class cast exception when casting Application to AppData in Android

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);
}

在 LISTVIEW 中显示列表时,当我点击选定的应用程序时,它应该显示应用程序包信息, 但是当我点击选择的应用程序时,应用程序自动关闭,获取错误日志,你能给我完美的解决方案吗?

getApplicationContext(); returns 上下文(在本例中为 Application class)而不是 AppData - 这就是您获得 ClassCastException 的原因。
您想要从 packageInfo 获取您刚刚从适配器获得的详细信息。

我尝试实现这样的功能,这是我作为启动器工作的代码。当您单击 GridView 中的一个项目时,您将启动一个应用程序,因此请深入了解我的示例:

public class MyHome extends Activity {
    private List<ResolveInfo> mApps;
    GridView mGrid;
    private OnItemClickListener listener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
            ResolveInfo info = mApps.get(position);

            //package names of all apps installed
            String pkg = info.activityInfo.packageName;
            //launching Activity's full name
            String cls = info.activityInfo.name;

            ComponentName componet = new ComponentName(pkg, cls);

            Intent i = new Intent();
            i.setComponent(componet);
            startActivity(i);
        }

    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        loadApps();
        setContentView(R.layout.main);
        mGrid = (GridView) findViewById(R.id.apps_list);
        mGrid.setAdapter(new AppsAdapter());

        mGrid.setOnItemClickListener(listener);
    }


    private void loadApps() {
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
    }

    public class AppsAdapter extends BaseAdapter {
        public AppsAdapter() {
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i;

            if (convertView == null) {
                i = new ImageView(MyHome.this);
                i.setScaleType(ImageView.ScaleType.FIT_CENTER);
                i.setLayoutParams(new GridView.LayoutParams(50, 50));
            } else {
                i = (ImageView) convertView;
            }

            ResolveInfo info = mApps.get(position);
            i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));

            return i;
        }

        public final int getCount() {
            return mApps.size();
        }

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

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

希望对您有所帮助。

我猜你正在创建一个名为 A​​ppData 的 class 扩展 A​​pplication class.So 你必须在您的 manifest 中定义 class。在清单文件的 Application 标记中写入:android:name="yourpackagename.AppData" 并且您的错误将得到解决。(注意:yourpackagename 是名称AppData 所在的包)

示例:

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