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;
}
}
}
}
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.