Android 动态更改片段上的线性布局

Android 动态更改片段上的线性布局,android,android-fragments,fragment,Android,Android Fragments,Fragment,我在ViewPager中有一个非常基本的片段。该片段包含线性布局。当我尝试调整linearlayout的内容时,我从linearlayout中得到一个nullpointer异常: //父活动(包含寻呼机) //碎片 public void LoadUnitContent(Unit latestUnit) { this.set_unit(latestUnit); if(latestUnit != null) { View v = this.getView(); //

我在ViewPager中有一个非常基本的片段。该片段包含线性布局。当我尝试调整linearlayout的内容时,我从linearlayout中得到一个nullpointer异常:

//父活动(包含寻呼机)

//碎片

public void LoadUnitContent(Unit latestUnit)
{
    this.set_unit(latestUnit);
    if(latestUnit != null) {
        View v = this.getView(); //-- this shows as null
        LinearLayout ll = (LinearLayout)v.findViewById(R.id.llExplanation);

    }
}
当我尝试从此方法访问onCreateView中设置的所有变量时,似乎它们现在都为空


任何帮助都将不胜感激。

我认为您会收到NullPointerException,因为((UnitFragment)this.\u cupAdapter.getItem(1))方法每次调用此方法时都会返回一个新创建的片段

因此,如果在它之后调用LoadUnitContent()方法,则片段尚未添加到ViewPager,并且片段的onCreate()方法从未被调用,因此视图为null

只需在方法中添加Log.d(“Fragment”、“onCreate”)和Log.d(“Fragment”、“LoadUnitContent”)即可查看是否首先调用了LoadUnitContent

public void LoadUnitContent(Unit latestUnit)
{
    this.set_unit(latestUnit);
    if(latestUnit != null) {
        View v = this.getView(); //-- this shows as null
        LinearLayout ll = (LinearLayout)v.findViewById(R.id.llExplanation);

    }
}