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

While Displaying List in LISTVIEW , when i click on the selected app, it should display application package info, but when i click on selected app, application closing automatically , getting error logs, will u sugest me perfect solution.

Error Log Information

Heshan Sandeepa
  • 3,388
  • 2
  • 35
  • 45
Anwesh
  • 91
  • 1
  • 1
  • 12

3 Answers3

1

I guess you are creating a class named AppData that extends the Application class.So you have to define the class in your manifest.Within the Application tag in the manifest file write: android:name="yourpackagename.AppData" and your error will be resolved.(Note: yourpackagename is the name of the package where your AppData resides)

Example:

<application
        android:name="yourpackagename.AppData"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
kgandroid
  • 5,507
  • 5
  • 39
  • 69
0

getApplicationContext(); returns Context(in this case Application class) not AppData - that's why you getting ClassCastException.
You want to get details from packageInfo which you just have obtained from adapter.

0

I tried to implement functions like this, here is my codes which works as a Launcher. When you click one item in the GridView, you will launcher an App, so take a deep look into my example:

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

I hope this will help you.

SilentKnight
  • 13,761
  • 19
  • 49
  • 78