1)我的Activity有很多片段,片段可以通过导航抽屉切换到另一个(整个rootView更改)。
2)我在一个片段中有一个listView,它使用简单的适配器进行更新。
3)如果我仍在加载listView时将片段切换到另一个片段。 - 发生错误。
我可以知道如何为此做出例外吗?
我为listView
扩展了简单的适配器.htaccess
这是自定义适配器:
ListAdapter adapter = new ExtendedSimpleAdapter(
getActivity(), news_List,
R.layout.news_item, new String[]{"news", "datetime", "desc","day"},
new int[]{R.id.news, R.id.new_time, R.id.desc,R.id.day});
setListAdapter(adapter);
错误消息NullPointerException
public class ExtendedSimpleAdapter extends SimpleAdapter{
Context context2;
public ExtendedSimpleAdapter(Context context, List<? extends Map<String, String>> data, int resource, String[] from, int[] to){
super(context, data, resource, from, to);
context2=context;
}
public View getView(int position, View convertView, ViewGroup parent){
// here you let SimpleAdapter built the view normally.
View v = super.getView(position, convertView, parent);
// Then we get reference for Picasso
ImageView img = (ImageView) v.getTag();
TextView txt = (TextView) v.findViewById(R.id.state);
if(img == null){
img = (ImageView) v.findViewById(R.id.new_pic);
v.setTag(img);
}
// get the url from the data you passed to the `Map`
String url = ((Map<String, String>)getItem(position)).get("pic");
String type = ((Map<String, String>)getItem(position)).get("type");
String state = ((Map<String, String>)getItem(position)).get("state");
if (type.equals("reward"))
{
txt.setVisibility(View.VISIBLE);
}
else
{
txt.setVisibility(View.GONE);
}
if(url.equals("")){
Picasso.with(context2).load(R.drawable.default).into(img);
}
else{
Picasso.with(context2)
.load(url)
.placeholder(R.drawable.default)
.resize(100, 100)
.centerCrop()
.into(img);
}
// return the view
return v;
}
}
这是否意味着当我继续加载listView时切换片段,适配器无法找到列表视图?
如何为此添加例外???