Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

app_name cannot be resolved or is not a field in eclipse android

The following is my code and for packageList.get(i).appName iam getting app_name cannot be resolved or is not a field error.

public class Applications
{
        private ArrayList packageList = null;
        private List activityList = null;
        private Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        private PackageManager packMan = null;

        public Applications(PackageManager packManager){
            packMan = packManager;
            packageList = this.createPackageList(false);
            activityList = this.createActivityList();
            this.addClassNamesToPackageList();
                }

        public ArrayList getPackageList(){
            return packageList;
               }

        public List getActivityList(){
            return activityList;
            }

        private ArrayList createPackageList(boolean getSysPackages){
            ArrayList pList = new ArrayList();        
               List packs = getPackageManager().getInstalledPackages(0);
               for(int i = 0; i < packs.size(); i++){
                    PackageInfo packInfo = (PackageInfo)packs.get(i);
                        if((!getSysPackages) && (packInfo.versionName == null)){
                     continue ;
                    }

                AppInfo newInfo = new AppInfo();
                newInfo.appName = packInfo.applicationInfo.loadLabel(
                getPackageManager()).toString();
                pList.add(newInfo);
            }
            return pList; 
        }

        private void packageDebug(){
            if(null == packageList){
                return;
            }

            for(int i = 0; i < packageList.size(); ++i){
Log.v("PACKINFO: ", "\t" + packageList.get(i).appName +   "\t"); 

            }
 }

Where I went wrong? I am Trying to build an application launcher and packageDebug function is for debugging purpose. List createActivityList() ia as follows.


private List createActivityList(){
            List aList = packMan.queryIntentActivities(mainIntent, 0);

            Collections.sort(aList, 
                    new ResolveInfo.DisplayNameComparator(packMan)); 
            ActivityInfo activ =new ActivityInfo();
            return aList;
        }

And i am getting error in:-


private void addClassNamesToPackageList(){
                if(null == activityList || null == packageList){
                    return;
                }

                String tempName = "";

                for(int i = 0; i < packageList.size(); ++i){
                    tempName = ((AppInfo)packageList.get(i)).packageName;

                for(int j = 0; j < activityList.size(); ++j){
                        if(tempName.equals(activityList.get(j).activityInfo.applicationInfo.packageName)){
                            packageList.get(i).className = activityList.get(j).activityInfo.name;
                        }
                }
                }
            }
like image 754
Sag Avatar asked Feb 19 '26 03:02

Sag


1 Answers

This is because of Generics. You haven't declared the parameterized type of ArrayList packageList instance member. If you don't declare it specifying the type like ArrayList<AppInfo>, then ArrayList.get(i) would return an instance of Object. You need to typecast it to AppInfo to get back the original instance underlying to call its methods. Make it to

((AppInfo)packageList.get(i)).appName

to get the appName value in the instance.

I completely agree with @Andrew T, Please change your declaration to ArrayList<AppInfo> to ensure type safety with Generics. So, that you don't have to worry about casting the objects in the list

like image 105
Keerthivasan Avatar answered Feb 20 '26 16:02

Keerthivasan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!