[Solved] How i can use findViewById() in Android?


If you’re trying to use findViewById in Fragment, first of all make sure you’re using onCreateView() and you’re returning the view at the end.

See the differences in here: Difference and uses of onCreate(), onCreateView() and onActivityCreated() in fragments

Here is an example for that:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_single_image, parent, false);

    alarm_timepicker = (TimePicker) rootView.findViewById(R.id.TimePicker); 
    // your widgets with rootView prefix

    return rootView;
}

rootView is the point here. So, if you want to declare any widgets (or etc), use rootView in this case as a prefix before referecning your id by findViewById.

1

solved How i can use findViewById() in Android?