findViewById returns NULL when using Fragment

前端 未结 7 1831
终归单人心
终归单人心 2020-12-14 16:15

I\'m new to Android developing and of course on Fragments.

I want to access the controls of my fragment in main activity but \'findViewById\' returns null. without f

相关标签:
7条回答
  • 2020-12-14 16:53

    1) Try this:

    Eclipse menu -> Project -> Clean...

    update

    2) If you have 2 or more instances of 'main' layout, check if all of them have a view with 'txtXML' id

    3)

    A Fragment is a piece of an application's user interface or behavior that can be placed in an Activity. Interaction with fragments is done through FragmentManager, which can be obtained via Activity.getFragmentManager() and Fragment.getFragmentManager().

    The Fragment class can be used many ways to achieve a wide variety of results. It is core, it represents a particular operation or interface that is running within a larger Activity. A Fragment is closely tied to the Activity it is in, and can not be used apart from one. Though Fragment defines its own lifecycle, that lifecycle is dependent on its activity: if the activity is stopped, no fragments inside of it can be started; when the activity is destroyed, all fragments will be destroyed.

    Study this. you must use FragmentManager.

    0 讨论(0)
  • 2020-12-14 17:02

    Try like this on your fragments on onCreateView

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
        if (container == null) {
            return null;
        }
    
        LinearLayout ll = (LinearLayout )inflater.inflate(R.layout.tab_frag1_layout, container, false);
    EditText txtXML = (EditText) ll.findViewById(R.id.txtXML);
    
        return ll;
    }
    
    0 讨论(0)
  • 2020-12-14 17:03

    All the answers above tell you how you should "return the layout" but don't exactly tell you how to reference the layout that was returned so I was unable to use any of the solutions given. I used a different approach to solve the problem. In the Fragment class that handles the fragment, got to the onViewCreated() class and create a context variable in it that saves the context of the parent activity (main activity in my case).

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Context fragmentContext = (MainActivity) view.getContext();
    }
    

    Once that is done, you can use the new context to access items on your fragment from inside the onViewCreated() method.

    EditText editText = context.findViewById(R.id.textXML);
    
    0 讨论(0)
  • 2020-12-14 17:05

    You can't access the the view of fragment in activity class by findViewById instead what you can do is...

    You must have an object of Fragment class in you activity file, right? create getter method of EditText class in that fragment class and access that method in your activity.

    create a call back in Fragment class on the event where you need the Edittext obj.

    0 讨论(0)
  • 2020-12-14 17:10

    You should inflate the layout of the fragment on onCreateView method of the Fragment then you can simply access it's elements with findViewById on your Activity.

    In this Example my fragment layout is a LinearLayout so I Cast the inflate result to LinearLayout.

        public class FrgResults extends Fragment 
        {
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
            {
                //some code
                LinearLayout ll = (LinearLayout)inflater.inflate(R.layout.frg_result, container, false);
                //some code
                return ll;
            }
        }
    
    0 讨论(0)
  • 2020-12-14 17:14

    I'm late, but for anyone else having this issue. You should be inflating your view in the onCreateView method. Then override the onCreateActivity method and you can use getView().findViewById there.

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup     container, Bundle savedInstanceState) 
    {
         return      inflater.inflate(R.layout.fragment, container, false);
    }
    
    0 讨论(0)
提交回复
热议问题