无法访问onclick的viewpager片段的子元素

时间:2013-12-24 08:38:18

标签: android onclick android-viewpager

在我的片段类中使用viewpager我尝试在点击片段上生成一些动画imageButton

我的片段类是

public class Fragment1 extends Fragment {
ImageButton btn;
Context cxt;

RelativeLayout fragment1;

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    if (container == null) {

        return null;
    }
    View v = inflater.inflate(R.layout.fragment1, container, false);

            btn = (ImageButton) v.findViewById(R.id.bJ);


    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            ////some animations             
        }
    });


            return (RelativeLayout) inflater.inflate(R.layout.fragment1,
            container, false);
}

} 但我的onclick事件没有生成,第一次使用viewpager,pl指南,而不是

2 个答案:

答案 0 :(得分:2)

您没有返回查看您处理点击的内容,  试试这个,

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    if (container == null) {

        return null;
    }
    View v = inflater.inflate(R.layout.fragment1, container, false);

            btn = (ImageButton) v.findViewById(R.id.bJ);


    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            ////some animations             
        }
    });


            return v;
}

答案 1 :(得分:1)

更改

return (RelativeLayout) inflater.inflate(R.layout.fragment1,
            container, false);

return v;