Android片段-onCreateView-容器为空
我对onCreateView方法有一个小问题-给定的容器/父对象为null,因此我无法膨胀布局。这是我的密码: 这是我的“主课” 这是我的activity_drawer.xmlAndroid片段-onCreateView-容器为空,android,android-fragments,Android,Android Fragments,我对onCreateView方法有一个小问题-给定的容器/父对象为null,因此我无法膨胀布局。这是我的密码: 这是我的“主课” 这是我的activity_drawer.xml <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" androi
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent" >
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<include layout="@layout/include_drawer"/>
<include layout="@layout/include_toolbar"/>
</android.support.v4.widget.DrawerLayout>
my activity_overview.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.xxxx.ui.overview.OverviewFragment"
android:id="@+id/fragment_overview" />
谁能解释一下,为什么容器是空的?谢谢……:) 使用静态方法时,您的
需要包含在视图组中。只需将一个RelativeLayout
作为其父级放在XML文件中即可。据我所知,视图组就是包含布局的视图。在许多情况下,它可以是空的,也不是什么大问题。您可以将参数attach to root更改为FALSE,例如:充气机.inflate(R.layout.fragment\u overview,container,FALSE);谢谢你能解释一下为什么我必须把它放在RelativeLayout中吗?片段在实例化时需要一个父视图组。因此,当静态使用它们时,它们必须包含在视图组(布局)中。您可以使用任何可用的布局,不仅仅是RelativeLayout
。但是抽屉布局是一个视图组,我不知道为什么会出现问题。我正在xml中添加RelativeLayout作为父级,但容器仍然为空
public class OverviewActivity extends DrawerActivity {
OverviewFragment overview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createMenuDrawer(R.layout.activity_overview);
}
}
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.xxxx.ui.overview.OverviewFragment"
android:id="@+id/fragment_overview" />
public class OverviewFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_overview, container, false);
}
}