Question

I was trying to implement onClick listener in fragments. when i add

           button1.setOnClickListener (this)

is getting force close.

Fragment code
Stack Trace

private  class PlaceholderFragment extends Fragment  implements OnClickListener{
     Button b1;
private   PlaceholderFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
      b1  = (Button) container.findViewById(R.id.button1);
     b1.setOnClickListener(PlaceholderFragment.this);

        return rootView;
    }

    @Override
    public void onClick(View arg0) {

        // TODO Auto-generated method stub

    }
}
Was it helpful?

Solution

This

 b1  = (Button) container.findViewById(R.id.button1);

Should be

b1  = (Button) rootView.findViewById(R.id.button1);

You inflate the layout and you need to use the view object to initialize views.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top