我正在广泛地学习吸气剂和制定者,但我似乎没有按照自己的方式行事。
我有一个名为Apps_Info的类,其中包含我的setter和getter,我的主要活动FavouriteApps有一个使用Apps_Info类的列表。
我试图从FavouriteApps中的List获取包的名称,但我仍然是null
请有人告诉我该怎么办?以下是此顺序中的代码:class Apps_Info和FavouriteApps活动
public class Apps_Info {
private Bitmap bIcon;
private String sName;
private String sPacks_Name;
public Apps_Info(Bitmap icon, String name, String Packs_Name) {
bIcon = icon;
sName = name;
sPacks_Name = Packs_Name;
}
public void setIcon(Bitmap icon) {
bIcon=icon;
}
public Bitmap getIcon() {
return bIcon;
}
public void setName(String name) {
sName=name;
}
public String getName() {
return sName;
}
public void setPacks_Name(String Packs_Name) {
this.sPacks_Name=Packs_Name;
}
public String getPacks_Name() {
return sPacks_Name;
}
}
FavouriteApps活动代码(部分)
String packname, packsname, apps_names;
Bitmap app_icon;
Resources res = getResources();
List<Apps_Info> ListApps_Info = new ArrayList<Apps_Info>();
ListApps_Info.add(new Apps_Info(BitmapFactory.decodeResource(res, R.drawable.browser_app), "Browser", "com.browser"));
ListApps_Info.add(new Apps_Info(BitmapFactory.decodeResource(res, R.drawable.clock_app), "Alarm Clock", "com.alarm.clock"));
ListApps_Info.add(new Apps_Info(BitmapFactory.decodeResource(res, R.drawable.threegplus), "3G Secure Connection", "threeg.secureconnect"));
mGridView.setAdapter(new Apps_Info_Adapter(this, ListApps_Info));
Apps_Info packinfo = new Apps_Info(app_icon, apps_names, packname);
packsname = packinfo.getPacks_Name();
apps_names = packinfo.getName();
Log.i("The Pack_Name is " + packsname, "Pack Name");
答案 0 :(得分:1)
这是因为您从未在代码中初始化packname
变量。
String packname,packsname,apps_names;
执行此操作时:
Apps_Info packinfo=new Apps_Info(app_icon, apps_names,packname);
packsname=packinfo.getPacks_Name();
你的getter表现不错,如果它返回null,这是正常的。
答案 1 :(得分:1)
你应该这样做。
for (int i = 0; i < ListApps_Info.size(); i++) {
Apps_Info packinfo = ListApps_Info.item(i);
packsname = packinfo.getPacks_Name();
apps_names = packinfo.getName();
Log.i("The Pack_Name is " + packsname, "Pack Name");
}