29

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?

waqaslam
  • 67,549
  • 16
  • 165
  • 178
mammadalius
  • 3,263
  • 6
  • 39
  • 47

7 Answers7

21

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;
}
Ufuk Doganca
  • 211
  • 2
  • 2
18

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;
        }
    }
mammadalius
  • 3,263
  • 6
  • 39
  • 47
  • 1
    Also make sure that if you are using support v4 for Android you use `FragmentActivity` and `getSupportFragmentManager` to avoid getting a `null`. If you are building only for newer versions of Android api then use a regular `Activity` and the method `getSupportFragmentManager`. do *not* mix these two ways of implementation – George Pligoropoulos Mar 22 '14 at 14:40
6

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);
}
e4c5
  • 52,766
  • 11
  • 101
  • 134
gdi
  • 85
  • 6
  • You are right. My problems is solved before. Please show some snippet for inflating that others can use. – mammadalius Jun 19 '12 at 09:38
  • `@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment, container, false); }` – gdi Jun 19 '12 at 18:12
  • Dear @Futan Please write your comment as a separate answer with clear description to be useful for other and of course I can select it as the Accepted Answer. – mammadalius Jun 19 '12 at 18:44
3

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.

Nixit Patel
  • 4,435
  • 5
  • 30
  • 57
2

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.

Bob
  • 22,810
  • 38
  • 143
  • 225
  • Do you have only one instance of "main" layout? – Bob May 06 '12 at 10:05
  • yes only one instance and I have to add this comment that `txtXML` is not on my `main` layout it's on another layout which I want to use in fragment. – mammadalius May 06 '12 at 10:14
  • on getting `Fragment` again the `null` is returned. `View myFragment = findViewById(R.layout.tab_frag2_layout);` – mammadalius May 06 '12 at 10:32
0

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

Moesio
  • 3,100
  • 1
  • 27
  • 36
0

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);
Kenneth Murerwa
  • 778
  • 12
  • 16