在使用fragement的时候,我个人比较粗心,出现了很多问题,写下来可能对大家有用。
一空指针问题
1)在使用findviewbyid的时候可能后面的资源是错的,可能是名字错了,
2)在写一些东西的时候,后面用到的东西一定先在前面声明。
3)在给listview写适配器的时候,map的一一对应关系要对应
simpAdapter=new SimpleAdapter
(getActivity(),getData(), R.layout.artical_listview, new String[]{"pic","title","pointword"},
new int[]{R.id.img,R.id.tv_title,R.id.tv_pointword});
和
Map<String, Object>map=new HashMap<String, Object>();
map.put("pic", R.drawable.imgnull);
map.put("title","题目:"+title );
map.put("pointword","关键词:"+pointword );
中间对应的pic","title","pointword“要对应,否则也会报空指针的错误
二点击事件无响应问题
<pre class="java" name="code">public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
findview();
init();
return inflater.inflate(R.layout.post, container, false);
}
事件监听在init方法里。然而并没有什么用。也没有报错。原因在这个地方
return inflater.inflate(R.layout.post, container, false);
即使在前面的init()中定义了很多。但是最终返回的确是一个原始的界面。所以事件无响应
修改如下
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
mView=inflater.inflate(R.layout.post,null);
findview();
init();
return mView;
}