Edward Wong
Edward Wong

Reputation: 41

Null view after replacing fragment

Here's the problem
I have a Fragment class DisplayFragment and I already have one show in the content frame, then I do

DisplayFragment a = DisplayFragment.newInstance();
getSupportFragmentManager().beginTransaction()
            .replace(R.id.contentFrame, DisplayFragment)
            .commit();

Then I want to get the view of fragment a using View v = a.getView();, but it return a null view.
Can anyone tell me why? Cause I have to change some view setting of the new Fragment.

onCreateView() in DisplayFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_display, container, false);
    //Some TextView setup
    Button button = (Button) view.fineViewById(R.id.button);
    return view; }

Upvotes: 3

Views: 1811

Answers (4)

Shivdas Bachewar
Shivdas Bachewar

Reputation: 1

I had same problem as like you. As the transaction of fragment did not takes place immediately. Thats why sometime the findViewById() does not work properly. But we can execute the pending transaction by using executePendingTransactions() method. I have used following code in my project to execute the pedingTransactions.

Fragment f = (Fragment)(FragmentClass.class).newInstance();

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.your_fragment_container_name,f).commit();
fm.executePendingTransactions(); //Notice the FragmentManager Class object

Upvotes: 0

Neeraj Singh
Neeraj Singh

Reputation: 668

Declare DisplayFragment a; as Class Reference. Because after commit you want to just access View v = a.getView(); it unavailable due to fragment Life Cycle. You can get after execute block of code.

Make sure you have to mentioned OnCreateView() Method on class DisplayFragment

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
  if (container == null) {
        // We have different layouts, and in one of them this
        // fragment's containing frame doesn't exist.  The fragment
        // may still be created from its saved state, but there is
        // no reason to try to create its view hierarchy because it
        // won't be displayed.  Note this is not needed -- we could
        // just run the code below, where we would create and return
        // the view hierarchy; it would just never be used.
        return null;
    }
    Log.i("Right", "onCreateView()");
    return inflater.inflate(R.layout.right, container, false);
} 

Upvotes: 0

Pratik
Pratik

Reputation: 30855

You need to use delay or put your View v = a.getView(); in loop which will watch first that your fragment is successfully attached, created and added in your activity or not. For this you can check with this isAdded() and isInLayout() if both return true then only call getView()

Now why this, as you add/replace fragment with commit with will fragment will class will be execute first it'll be start Fragment life cycle that is onAttach(), onCreate(), onCreateView() and so on. Now you will getting null from getView() just because your view is not created still. Fragment Life Cycle. If you doubt regarding this let me know.

Upvotes: 1

justHooman
justHooman

Reputation: 3054

I assume that you call View v = a.getView(); right after commit?

Because getView method only return not null value after onCreateView returned.

In your case, after called commit(), it take time to complete all the lifecyle callback of DisplayFragment a (from onCreate -> onCreatedView,..).
So that, right after commit(), the getView method still return null.

Upvotes: 0

Related Questions