我想要做的是点击列表视图的项目从另一个片段调用。程序构建没有错误,但点击项目时崩溃。顺便说一下,我在这里制作了局部Fragment_3对象,我已经在我的mainactivity中完成了它,如何将它传递给这个函数?
Fragment_1.java内容:
public class Fragment_1 extends SherlockFragment implements OnItemClickListener{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = inflater.inflate(R.layout.fragment_1, container, false);
ListView listView = (ListView) view.findViewById(R.id.listView1);
listView.setOnItemClickListener(this);
return view;
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
Fragment Fragment3 = new Fragment_3();
Integer fragmentId = (Integer) v.getTag();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(fragmentId, Fragment3);
fragmentTransaction.commit();
}
}
答案 0 :(得分:0)
我怀疑v.getTag()
是空的;所以Integer fragmentId = (Integer) v.getTag();
最终会在fragmentId中为null。将其拆箱到int时,您将获得NullPointerException
。为什么不用fragmentId
中的fragmentTransaction.replace(fragmentId, Fragment3);
替换包含当前Fragment_1
实例的容器的id?