getView()返回null [英] getView() returns null

查看:865
本文介绍了getView()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我basicall有一个的AsyncTask (从主活动执行)的填充一个 ViewPager 片段内。我膨胀的XML布局文件来填充 ViewPager

I basicall have an AsyncTask (run from main Activity) that populates a ViewPager inside a fragment. I'm inflating an xml layout file to populate the ViewPager.

问题是,我不能让指针布局(ImageView的,TextView的)内部的意见,让我可以在运行时再改。我知道这可能是因为 getView()返回。我读过,这可能是因为 onCreateView()没有被调用,但。任何人都知道我能做些什么来解决这个?

The problem is that I can't get pointers to the views inside the layout (imageview, textview) so that I can change then at runtime. I know it's probably because getView() returns null. I've read that it's probably because onCreateView() hasn't been called, yet. Anybody know what I can do to solve this?

我的code是一个有点乱,现在。

My code's a bit of a mess right now.

下面是一个简单的解释,我在做什么:

Here's a simpler explanation to what I'm doing:

  1. MainActivity AsyncTask的填充数据库,并将指针片段。喜欢的东西SendToFragement(DB);
  2. 在该片段的方法ReceiveFromActivity(DB)接收DB指针和填充ViewPager。

它工作正常,如果我只是创建TextViews,设置文本,并添加TextViews到Viewpager。但是,当然,我想使它看起来更好,所以我膨胀的XML布局。问题是,我不能改变的XML布局的内容,因为 getView()将返回 NULL 那么 getView()。findViewById()不起作用。

It works fine if I'm just creating TextViews, setting text, and adding TextViews to the Viewpager. But, of course, I want to make it look better so I've inflated an xml layout. The problem is that I can't change the contents of the xml layout because getView() is returning NULL so getView().findViewById() doesn't work.

推荐答案

根据您的PagerAdapter的实施,有可能是一个片段不再考虑将它们的查看破坏,以释放资源。因此,没有必要更新查看,因为它会再次在 onCreateView创建()信息更新。 getView()将返回调用之间,以便 onCreateView() onDestroyView() 。那之外,片段仍然可以存在于内存,但不依附于任何查看,从而 getView()返回

Depending on the implementation of your PagerAdapter, it is possible that a Fragment no longer in view will have their View destroyed in order to free up resources. As such, there's no need to update the View because it will be created again in onCreateView() with the updated information. getView() will return a view in between calls to onCreateView() and onDestroyView(). Outside of that, the Fragment can still exist in memory, but not attached to any View, thus getView() will return null.

因此​​,基本上,

View fragmentView = getView();
if(fragmentView != null) {
  // we are in view or at least exist. Update the elements.
}
// Else don't worry about it. Just update the data.

这篇关于getView()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆