getView返回null时的片段被从活动中创建 [英] getView returning null when fragment has been created from an activity
问题描述
我有一个工作的平板电脑的应用程序,我现在正在努力使在手机上工作过。 在一个桌子上有屏幕上的两个片段列表片段和细节片段。 当在电话列表中的片段出现,并创建一个新的活动时,列表项是pssed $ P $。 该活动只是会在onCreate方法的片段,并将其提交到屏幕上,如下所示。
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();
这是正在从这个活动我要告诉哪些细节要加载和显示的片段中如预期但是。在我DealDetailsFragment类我有一个更新的内容如下:a updateDeal方法。
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()被返回null当应用程序运行在一个电话那里只示出一个单一片段。
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 avail bile online.
推荐答案
将你的方法调用过程中 onCreateView
来执行,并使用您正在膨胀,以供参考,而不是视图 getView的()
。见片段生命周期的详细信息:<一href="http://developer.android.com/guide/topics/fundamentals/fragments.html#Creating">http://developer.android.com/guide/topics/fundamentals/fragments.html#Creating
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: http://developer.android.com/guide/topics/fundamentals/fragments.html#Creating
这篇关于getView返回null时的片段被从活动中创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!