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

时间:2015-03-30 08:50:03

标签: android android-applicationinfo

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中显示列表时,当我点击所选应用时,它应显示应用包信息, 但是当我点击选定的应用程序,应用程序自动关闭,获取错误日志时,你会给我一个完美的解决方案。

Error Log Information

3 个答案:

答案 0 :(得分:1)

我猜您正在创建一个名为 AppData 的类,它扩展了应用程序类。因此,您必须在清单中定义该类。在清单文件中的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" >

答案 1 :(得分:0)

getApplicationContext();返回Context(在本例中为Application类)而不是AppData - 这就是为什么你得到ClassCastException。
您想从packageInfo获取您刚从适配器获得的详细信息。

答案 2 :(得分:0)

我试图实现这样的功能,这是我的代码,它起着Launcher的作用。当您单击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;
        }
    }
}

我希望这会对你有所帮助。