findViewById returns NULL when using Fragment

℡╲_俬逩灬. 提交于 2019-11-28 21:14:11

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;
}

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;
        }
    }
gdi

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);
}

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.

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.

If you want use findViewById as you use at activities onCreate, you can simply put all in overrided method onActivityCreated.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!