-1

Can someone tell me how to set a button in tabbed fragment layout to open a new activity? If I set a button in a tab then it shows error.

enter image description here

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

2 Answers2

1

You have to save the inflated view in a variable, then use findViewById on that view to find the textview and button, then at the end, return the inflated view.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_g, container, false);

    TextView tv = (TextView) root.findViewById(R.id.textView);
    Button btn = (Button) root.findViewById(R.id.button);

    //Other code here

    return root;
}

You're misunderstanding some very fundamental things, such as writing code after the return statement. You might want to read some starter guides online.

Moonbloom
  • 7,738
  • 3
  • 26
  • 38
0

You can use below code

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


    View view = inflater.inflate(R.layout.activity_complete_registration, container, false);

    final TextView  tv=(TextView)view.findViewById(R.id.textView);
    final Button button=(Button)view.findViewById(R.id.button);     

   button.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {

  //Your required code here
     }
    });

return view;
    }
Amit Yadav
  • 406
  • 2
  • 11