从活动创建片段时,getView 返回 null [英] getView returning null when fragment has been created from an activity
问题描述
我有一个可以工作的平板电脑应用程序,我现在也想在手机上使用它.在一张桌子上,屏幕上有两个片段,一个是列表片段,一个是详细信息片段.当在手机上时,列表片段会出现并在按下列表项时创建一个新活动.此活动仅在 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 类中,我有一个 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 available online.
推荐答案
移动你的方法调用以在 onCreateView
期间执行,并使用你正在膨胀的视图而不是 getView()
.有关更多信息,请参阅片段生命周期:https://developer.android.com/guide/components/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: https://developer.android.com/guide/components/fragments.html#Creating
和 getView()
的文档解释了为什么它在 onCreateView(LayoutInflater, ViewGroup, Bundle)
返回之前返回 null:
and the documentation of getView()
that explains why it returns null before onCreateView(LayoutInflater, ViewGroup, Bundle)
returns:
getView()获取片段布局的根视图(由 onCreateView(LayoutInflater, ViewGroup, Bundle) 返回的视图)(如果提供).
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()
这篇关于从活动创建片段时,getView 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!