简体   繁体   English

从活动创建片段时,getView 返回 null

[英]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.此活动仅在onCreate()方法中创建片段并将其提交到屏幕,如下所示。

// 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.在我的 DealDetailsFragment 类中,我有一个updateDeal()方法,它更新内容如下。

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.当应用程序在只显示一个片段的手机上运行时, getView()返回 null。

Any ideas?有任何想法吗? Unfortunately, there is not many fragment examples available online.不幸的是,在线提供的片段示例并不多。

Move your method call to be executed during onCreateView , and use the view you are inflating for reference instead of getView() .移动您的方法调用以在onCreateView期间执行,并使用您正在膨胀的视图作为参考而不是getView() See the fragment lifecycle for more information: https://developer.android.com/guide/components/fragments.html#Creating有关更多信息,请参阅片段生命周期: 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:以及解释为什么它在onCreateView(LayoutInflater, ViewGroup, Bundle)返回之前返回 null 的getView()文档:

getView() Get the root view for the fragment's layout (the one returned by onCreateView(LayoutInflater, ViewGroup, Bundle)), if provided. getView()获取片段布局的根视图(由 onCreateView(LayoutInflater, ViewGroup, Bundle) 返回的视图)(如果提供)。

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

Moving the method to onCreateView() did not help me.so... Create a global variable mView将方法移动到onCreateView()对我没有帮助。所以...创建一个全局变量mView

protected View mView;

and in onCreateView()并在 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并将所有getView()替换为mView

You can fix that by putting your code inside the onViewCreated() -method, which you should override.您可以通过将代码放在onViewCreated()方法中来解决该问题,您应该覆盖该方法。 Don't forget to call super() on it.不要忘记调用super()

You must check the Android Lifecycle to understand WHY is it null on onAttach(...) function.您必须检查 Android Lifecycle 以了解为什么 onAttach(...) 函数为 null。

The first function called when fragment is created is added is onAttach() , but actually no view has been created.添加片段时调用的第一个函数是onAttach() ,但实际上还没有创建视图。 That's why null is returned when you try to access within this call.这就是当您尝试在此调用中访问时返回 null 的原因。

Next function is onCreate() ... but there is no view created yet!!!!下一个函数是onCreate() ...但是还没有创建视图!!!!

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.调用的第三个函数是onCreateView() ,在这里你必须指明哪个是附加到这个片段的视图......而且只有在这个调用中存在视图对象并且可以访问它。

Read and learn the full lifecycle here .在此处阅读并了解完整的生命周期。

Greetings你好

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 .因为 onCreate() 在 onCreateView() 之前被调用,所以无论你是否在 onCreateView() 中膨胀视图,你都不会通过 onCreate() 中的 getView() 得到任何东西,因为在那个时候, onCreate() 还没有被调用.

安卓生命周期

it becomes null basically because you call getview() before the view being inflated.它变为 null 基本上是因为您在视图膨胀之前调用了getview() Create a View object and fill using inflater and then find your UI element see code below创建一个View对象并使用inflater填充,然后找到您的 UI 元素,请参阅下面的代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 fragment.getview()从活动返回null - fragment.getview() returning null from activity 从不同片段调用时,片段上的getView返回null - getView on fragment returning null when called from different fragment 来自活动的getView片段? - getView fragment from activity? 片段getView()始终为FragmentStatePagerAdapter创建的片段返回null - Fragment getView() always returning null for Fragments created by a FragmentStatePagerAdapter 在活动中调用时fragment.getview()始终返回null - fragment.getview() always return null when called in an activity 片段getView()。findViewById更改方向时返回null - fragment getView().findViewById returning null when changing orientation ViewPager中的片段在getView()中返回null - Fragment inside ViewPager returning null in getView() 片段的getView()在OnClickListener回调中返回null - Fragment's getView() returning null in a OnClickListener callback 如何重新启动从 viewpager2 中的活动创建的片段? - How can I restart a fragment that has been created from the activity in viewpager2? Android片段,findViewById()返回null,但getView()返回正确的视图 - Android fragment, findViewById() returning null, but getView() returning correct view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM