2
public class MainFragment extends Fragment {
    public static final String TAG = MainFragment.class.getSimpleName();
    private static final String ABOUT_SCHEME = "settings";
    private static final String ABOUT_AUTHORITY = "main";
    public static final Uri ABOUT_URI = new Uri.Builder().scheme(ABOUT_SCHEME).authority(ABOUT_AUTHORITY).build();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.mainbutton, container, false);

        return v;
    }

}

According to below link: How to handle button clicks using the XML onClick within Fragments

public class StartFragment extends Fragment implements OnClickListener{


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_start, container, false);

        Button b = (Button) v.findViewById(R.id.StartButton);
        b.setOnClickListener(this);
        return v;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.StartButton:

                /* ... */

                break;
        }
    }
}

source doesn't work too.

Community
  • 1
  • 1
Alex
  • 21
  • 1
  • 1
  • 2
  • 2
    There is no reason why the second half of code shouldn't work unless the button isn't found which would have thrown a NullPointerException. The first section of code is useless. – JRomero Jun 20 '13 at 18:13
  • 1
    What @J.Romero said is exactly right. The code looks exactly as it should. How are you sure your click listener isn't being triggered? – dymmeh Jun 20 '13 at 18:29
  • In addition could you post the entire layout (xml), there could be a layering problem. – JRomero Jun 20 '13 at 20:12

3 Answers3

4

@J.Romero is right. Try this code, I change the onClick method and add some debug log.

public class StartFragment extends Fragment implements OnClickListener {

    private static final LOG_TAG = "com.example.activity"

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_start, container, false);

        if (v != null) {
            Button mButton = (Button) v.findViewById(R.id.StartButton);
            Log.d(TAG, "View is not null");

            if (mButton != null) {
                b.setOnClickListener(this);
                Log.d(TAG, "mButton is not null");
            }
        }

        return v;
    }

    @Override
    public void onClick(View v) {
        if (v == mButton) {
            //do something
        }
    }
}
2

you can NOT access the UI element in onCreateView method - EVER

use onActivityCreated method , thats tell the fragment the activity is fully created and ready to interact

 @Override
        public void onActivityCreated(Bundle savedInstanceState) 
        {
            // TODO Auto-generated method stub
            super.onActivityCreated(savedInstanceState);

            Button b = (Button) v.findViewById(R.id.StartButton);
            b.setOnClickListener(new OnClickListener() {

               @Override
                public void onClick(View v) {
                                    }
                            });
        }
chazrmani
  • 122
  • 11
  • 1
    Actually, you can access the UI elements in the onCreateView method. I just tried it, and I get the EditText and do a .setText(...) on it, and it works perfectly. The click event, however, does not work. – Ted Jul 20 '15 at 17:10
  • Also, where do you get the 'v' varible from? – Ted Jul 20 '15 at 17:14
-1

Try this way:

Button b = (Button) v.findViewById(R.id.StartButton);
b.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        /*Your code*/   
                        }
                });