Findviewbyid in Fragment

Cannot resolve 'findViewById' method in Fragment RecyclerView

Add these variables:

View root;
RecyclerView recyclerView;

Change your onCreateView to this:

public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_profile, container, false);
recyclerView = root.findViewById(R.id.recyclerView);
return root;
}

and lastly - make sure that your recyclerview within your XML file has the following:

 android:id="@+id/recyclerView"

Cannot resolve findViewById in fragment

Firstly, you are calling findViewById() after the return statement so they wont be executed at all. Next, you need to call findViewById() on the context of a view as in view.findViewById(int); so rewrite the code as :

View v = inflater.inflate(R.layout.activity_slide__teacher_add,container,false);
teacher_id_number = v.findViewById(R.id.teacher_id_number); //Note this line
//other tasks you need to do
return v;

making sure the return is the last statement of the function.

How do I use findViewById to get a ViewGroup outside OnCreateView in a Fragment?

Your issue stems from a misunderstanding about when and for how long a fragment's view exists.

Currently, you are assigning your MyLayout variable during construction of your fragment.

According to the Android documentation on a Fragment's lifecycle a fragment won't have a view associated with it until after onCreateView is called. Later on in the fragment's lifecycle, the view is destroyed when onDestroyView is called.

So, the fragment's view only lives during the intervening time between onCreateView and onDestroyView. If you call getView before onCreateView is called, or after onDestroyView is called, you will get null.

So, if you want to set listeners on views, do so either from onCreateView or onViewCreated and remove them in onDestroyView.

Also, if you want to hold onto your view via a member variable, set it in onCreateView and null it out in onDestroyView and any place you reference it, make sure to check for null first.

How to access findViewById in Fragment view - Kotlin

You can try this approach.

lateinit var progressBar: ProgressBar // Global declare

Then

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
progressBar = view.findViewById(R.id.loader)
// You can declare your listview here
fetchJsonData().execute()
}

Then

 override fun onPostExecute(result: String?) {
super.onPostExecute(result)
progressBar.visibility = View.GONE

findViewById in fragment without put in onCreateView

First about inflating views:

This code here:

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

"Inflates" the View which means that it takes xml file and just create all those objects. So after that v is a view that contains all other views defined in your xml. That is why you can use:

(TextView) v.findViewById(R.id.awayname);

Because this TextView is definec inside View.

getView() and getActivity() difference

Another thing is that your Fragment is "hosted" by Activity. It also has it's View (inflated by usage of setContentView(...) method). By this you can use Activity.findViewById() and you will be able to find views inflated in Activity's xml file (set in setContentView()).
That is why:

(TextView) v.findViewById(R.id.awayname);

works, and

(TextView) getActivity().findViewById(R.id.awayname);

doesn't. Activity simply hasn't inflated the view containing R.id.awayname.
So as you see: getActivity() returns you just the activity that is "hosting" fragment, and getView() returns the view that you've returned from onCreateView() method.

Why getView() produces null?

It's because you've used getView() method before onCreateView() return statement.

What to do?

To make it work it's wise to create properties inside of your Fragment/Activity like this:

public class TabMatchFragmentOdd extends Fragment {
TextView Home, Away;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v;
v = inflater.inflate(R.layout.tab_match_odd, container, false);
getViews(v);
return v;
}

private void getIncomingIntent() {
(...)
setContent(HomeName,AwayName);
}

private void getViews(View v) {
Home = (TextView) v.findViewById(R.id.homename);
Away = (TextView) v.findViewById(R.id.awayname);
}

private void setContent(String home, String away) {
Home.setText(home);
Away.setText(away);
}


Related Topics



Leave a reply



Submit