我正在尝试从 Activity 的 onCreate() 方法初始化列表片段中的列表内容。
部分活动代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dish);
FragmentTransaction transaction1 = getSupportFragmentManager()
.beginTransaction();
mDishListFragment = new DishListFragment();
transaction1.add(R.id.dishList_fragment, mDishListFragment, "DishListFragment");
transaction1.commit();
mDishListFragment.showItem(totalList.get(0));
}
部分代码:
BinderData adapter = null;
List<HashMap<String, String>> dishDataCollection;
@Override
public void onCreate(Bundle savedInstanceState) {
Log.e("emenu", "Dishlistfragment on create");
super.onCreate(savedInstanceState);
adapter = new BinderData(getActivity(), R.id.article);
}
public void showItem(List<HashMap<String, String>> map) {
dishDataCollection = map;
if (dishDataCollection != null) {
adapter.init(dishDataCollection); //NullPOinterException
}
setListAdapter(adapter);
}
初始化自定义适配器时出现 nullpointerException。
现在,当我在活动中以不同的方式初始化我的片段时,这段代码可以正常工作。像这样:
mDishListFragment = (DishListFragment) getSupportFragmentManager()
.findFragmentById(R.id.dishList_fragment);
而不是像我在上面的第一个代码块中那样初始化。
我需要使用片段事务 add() 方法,因为我需要能够在需要时隐藏和显示片段。
两种类型的初始化有什么区别,为什么前者不起作用而后者起作用?
希望大家能帮忙。如果您需要代码的任何其他部分,请告诉我。
供您参考,适配器代码:
LayoutInflater inflater;
ImageView thumb_image;
List<HashMap<String, String>> dishDataCollection;
ViewHolder holder;
public BinderData(Context context, int list_row) {
// super(context , list_row , map);
super(context, list_row);
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void init(List<HashMap<String, String>> map) {
this.dishDataCollection = map;
}
public int getCount() {
// TODO Auto-generated method stub
// return idlist.size();
return dishDataCollection.size();
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView != null)
if (position == mItemIndex) {
convertView.setSelected(true);
convertView.setPressed(true);
convertView.setBackgroundResource(R.drawable.gradient_bg_hover);
} else {
convertView.setBackgroundResource(R.drawable.gradient_bg);
}
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.list_row, parent, false);
holder = new ViewHolder();
holder.name = (TextView) vi.findViewById(R.id.dish); // city name
holder.rate = (TextView) vi.findViewById(R.id.rate); // city weather
// overview
holder.shortdescription = (TextView) vi
.findViewById(R.id.shortdescription); // city temperature
holder.dishImage = (ImageView) vi.findViewById(R.id.list_image); // thumb
// image
// vi.setActivated(true);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
// Setting all values in listview
holder.name.setText(dishDataCollection.get(position).get(KEY_NAME));
holder.rate.setText(dishDataCollection.get(position).get(KEY_RATE));
holder.shortdescription.setText(dishDataCollection.get(position).get(
KEY_SHORT));
// Setting an image
String uri = "drawable/"
+ dishDataCollection.get(position).get(KEY_ICON);
int imageResource = vi
.getContext()
.getApplicationContext()
.getResources()
.getIdentifier(
uri,
null,
vi.getContext().getApplicationContext()
.getPackageName());
Drawable image = vi.getContext().getResources()
.getDrawable(imageResource);
holder.dishImage.setImageDrawable(image);
return vi;
}
/*
*
* */
static class ViewHolder {
TextView name;
TextView rate;
TextView shortdescription;
ImageView dishImage;
}
public void setSelectItem(int index) {
mItemIndex = index;
}
public int getSelectItem() {
return mItemIndex;
}