GetView returns null when fragment was created from action

I have an application for a working tablet, which I am now trying to make on phones as well. There are two fragments on the table on the screen of the list fragment and the fragment of the part. When a fragment of the list appears on the phone and a new action is created when you click on a list item. This operation simply creates a fragment in the onCreate() method and writes 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 works as expected, however, from within this activity I need to tell the fragment which details to load and display. In my DealDetailsFragment class, I have an updateDeal() method that updates the contents 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() returns null when the application is launched on the phone, where only one fragment is shown.

Any ideas? Unfortunately, there are not many examples of fragments on the Internet.

+48
android android-layout android-intent android-fragments
Feb 28 '12 at 19:44
source share
6 answers

Move the method call that will be executed during onCreateView , and use the view that you are inflating for the link instead of getView() . See Fragment Lifecycle for more information: https://developer.android.com/guide/components/fragments.html#Creating

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

GetView () Get the root view for the fragment layout (the one that was returned by onCreateView (LayoutInflater, ViewGroup, Bundle)), if provided.

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

+105
Feb 29 2018-12-12T00:
source share

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

  protected View mView; 

and in onCreateView ()

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, 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 replace all your getView() with mView

+17
Aug 17 '15 at 8:46
source share

You have to check the Android life cycle to see why it is zero onAttach (...).

The first function, called when creating the fragment, onAttach () , was added , but in fact no view was created. Therefore, when you try to access this call, null is returned.

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

The third function is called onCreateView () , and it is here where you must indicate which view is associated with this fragment .... And this is only from this call, where the view object exists and can be accessed.

Read and explore the full life cycle here .

Hello

+4
Oct 13 '15 at 16:37
source share

You can fix this by putting your code inside the onViewCreated() method, which you must override. Remember to call super() on it.

+4
Apr 01 '16 at 18:17
source share

it becomes null mainly because you call getview() before the view is inflated. Create a View object and fill it with inflater , and then find the user interface element, see the 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; 
0
Oct 16 '16 at 13:46 on
source share

Since the onCreate () function is called before onCreateView (), regardless of whether you inflate the view in onCreateView () or not, you will not get anything through getView () in onCreate (), because onCreate () has not yet been called .

Android Lifecycle

0
Apr 20 '17 at 6:09
source share



All Articles