0

我想要做的是在单击列表视图项目时从另一个片段中调用一个片段。程序构建没有错误,但它在单击项目时崩溃。顺便说一句,我在这里制作本地 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();

}

}
4

1 回答 1

0

我怀疑那v.getTag()是空的;所以Integer fragmentId = (Integer) v.getTag();最终会在 fragmentId 为空。将其拆箱为 int 时,您将得到一个NullPointerException. 为什么不将fragmentIdin替换fragmentTransaction.replace(fragmentId, Fragment3);为包含当前实例的容器的 id Fragment_1

于 2013-08-15T16:07:19.593 回答