You inflate Layout in Fragment and in Activity .
If you want to handle this Button in your Fragment , do the following:
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.activity_main, container, false); Button regButton1 = (Button) rootView.findViewById(R.id.btnRegister); regButton1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(getActivity(), "Example action", Toast.LENGTH_SHORT).show(); } }); return rootView; }
The code in your Activity looks good, but you fanned the same Layout for both.
Cm:
setContentView(R.layout.registration);
AND
View rootView = inflater.inflate(R.layout.registration, container, false);
Also, press CTRL + Alt + O to remove the unused import, in the end it should be like this (your Fragment import):
import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.Toast;
And, as I already guessed, the problem is that you uploaded Layout in Fragment and in Activity .and I tried this and worked with this Activity .
(good question, by the way).
Solution: Define your Layout for Fragment and Activity , but not the same Layout for both.
Then you are ready to go.