findViewById returns NULL when using Fragment

2019-01-22 19:53发布

问题:

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 fragment the code works fine.

Here's part of my code:

The fragment:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:ignore="HardcodedText" >

    <EditText
        android:id="@+id/txtXML"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:ems="10"
        android:scrollbars="vertical">
    </EditText>

</LinearLayout>

the onCreate of MainActivity:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.main);

        this.initialisePaging();

        EditText txtXML = (EditText) findViewById(R.id.txtXML);}

on this point the txtXML is null.

What's Missing in my code or what should I do?

回答1:

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


回答2:

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


回答3:

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


回答4:

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.



回答5:

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.



回答6:

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