-3

So I followed a tutorial online about creating a TabbedLayout in Android Studio. Everything went well and I did end of getting the TabbedLayout.

enter image description here

Above is my files. I have a java class and fragment for each tab, along with the MainActivity of course. But I don't know where to add code for each separate fragment or tab. I tried adding it to the corresponding java class but inside there is no onCreate method so I can't access findViewByID().

Below is what each tab java class looks like. enter image description here

I also tried doing it from the mainActivity (where I could access findViewByID) but my App kept force closing.

So I'm just not sure where to go from here. Any help would be appreciated. Thanks!

1 Answers1

0

use onViewCreated() after onCreateView

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_home, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    //here your code
    TextView text = view.findViewById(R.id.text);
}
ZeroOne
  • 8,996
  • 4
  • 27
  • 45
  • Ok thanks that works, however when creating an onClick method in the fragment, no object on the fragment can recognize the method. –  May 05 '17 at 16:09
  • make sure you have the correct id.. try to log inside the onclick – ZeroOne May 06 '17 at 02:26
  • I tried with no luck. The buttons just do the recognize the onclick –  May 06 '17 at 15:38