getView returning null when fragment has been created from an activity

AndroidAndroid LayoutAndroid IntentAndroid FragmentsAndroid 4.0-Ice-Cream-Sandwich

Android Problem Overview


I have a working tablet application which I am now trying to make work on phones too. On a table there is two fragments on the screen a list fragment and a details fragment. When on a phone the list fragment appears and creates a new activity when a list item is pressed. This activity simply creates the fragment in the onCreate() method and commits it to the screen as follows.

// Place an DeallDetailsFragment as our content pane
DealDetailsFragment f = new DealDetailsFragment();
getFragmentManager().beginTransaction().add(android.R.id.content, f).commit();
getFragmentManager().executePendingTransactions();

This is working as expected however from within this activity I need to tell the fragment what details to load and display. In my DealDetailsFragment class I have a updateDeal() method which updates the content as follows.

if (deal==null) { // could be null if user presses the loading deals list item before it loads
    return;
}
this.deal=deal;
if (dealTitle==null) { // get the view objects only once
    holder = new ViewHolder();	
    holder.dealHeat=(TextView) getView().findViewById(R.id.dealDetails_heat_textView);
    holder.dealPrice=(TextView) getView().findViewById(R.id.dealDetails_price_textView);
    holder.dealRetailer=(TextView) getView().findViewById(R.id.dealDetails_retailer_textView);
    holder.dealTitle=(TextView) getView().findViewById(R.id.dealDetails_title_textView);
    holder.dealDesc=(TextView) getView().findViewById(R.id.dealDetails_desc_textView);
    holder.goToButton= (LinearLayout) getView().findViewById(R.id.dealDetails_goToDeal);
    holder.dealImage=(ImageView) getView().findViewById(R.id.dealDetails_imageView);
    holder.postedBy=(TextView) getView().findViewById(R.id.dealDetails_poster_textView);
    holder.datePosted=(TextView) getView().findViewById(R.id.dealDetails_date_textView);

getView() is returning null when the application is ran on a phone where there is only a single fragment shown.

Any ideas? Unfortunately, there is not many fragment examples available online.

Android Solutions


Solution 1 - Android

Move your method call to be executed during onCreateView, and use the view you are inflating for reference instead of getView(). See the fragment lifecycle for more information: https://developer.android.com/guide/components/fragments.html#Creating

and the documentation of getView() that explains why it returns null before onCreateView(LayoutInflater, ViewGroup, Bundle) returns:

> getView() Get the root view for the fragment's layout (the one returned by onCreateView(LayoutInflater, ViewGroup, Bundle)), if provided.

https://developer.android.com/reference/android/app/Fragment.html#getView()

Solution 2 - Android

Moving the method to onCreateView() did not help me.so... Create a global variable mView

protected View mView;

and in onCreateView()

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  Log.d(TAG, "oncreateView");
  super.onCreateView(inflater, container, savedInstanceState);
    
  View view = inflater.inflate(R.layout.activity_secure_cloud_drive_folder, container, false);
  this.mView = view;
  return view;
}

and the replace all your getView() with mView

Solution 3 - Android

You can fix that by putting your code inside the onViewCreated()-method, which you should override. Don't forget to call super() on it.

Solution 4 - Android

You must check the Android Lifecycle to understand WHY is it null on onAttach(...) function.

The first function called when fragment is created is added is onAttach(), but actually no view has been created. That's why null is returned when you try to access within this call.

Next function is onCreate()... but there is no view created yet!!!!

The third function called is onCreateView(), and it's here where you have to indicate which is the view attached to this fragment.... And it's only from this call where a view object exists and can be accessed.

Read and learn the full lifecycle here.

Greetings

Solution 5 - Android

Because onCreate() is called before onCreateView() , whether you inflate a view in onCreateView() or not, you'll get nothing through getView() in onCreate(), because in that time , onCreate() has not been called yet .

Android Lifecycle

Solution 6 - Android

it becomes null basically because you call getview() before the view being inflated. Create a View object and fill using inflaterand then find your UI element see code below

  View view = inflater.inflate(R.layout.fragment_albumslist, container, false);
		TextView t= (TextView)view.findViewById(R.id.txtTest);
      	t.setText(strtext);
      	return view;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbencallisView Question on Stackoverflow
Solution 1 - AndroidJason RobinsonView Answer on Stackoverflow
Solution 2 - AndroidspaceMonkeyView Answer on Stackoverflow
Solution 3 - AndroidJoaquin IurchukView Answer on Stackoverflow
Solution 4 - AndroidAnibal ItriagoView Answer on Stackoverflow
Solution 5 - AndroidJack ShenView Answer on Stackoverflow
Solution 6 - AndroidCode_WormView Answer on Stackoverflow