Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

android - getView returning null when fragment has been created from an activity

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...