2014-12-05 63 views
3

我有一个onCreateView方法的小问题 - 给定的容器/父母为空,因此我不能膨胀我的布局。这里是我的代码:Android的片段 - onCreateView - 容器为空

这是我的 “主” 类

public class DrawerActivity extends ActionBarActivity 
     implements NavigationFragment.NavigationListener { 
    private static final int DRAWER_DELAY = 250; 

    private Toolbar toolbar; 
    private NavigationFragment navigationFragment; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // check if smartphone or tablet... 
     if (isStaticMenuDrawer()) { 
      setContentView(R.layout.activity_drawer_static); 
     } else { 
      setContentView(R.layout.activity_drawer); 
     } 

    } 

    protected void createMenuDrawer(int contentViewId) { 
     ViewGroup content = (ViewGroup) findViewById(R.id.content); 
     content.addView(getLayoutInflater().inflate(contentViewId, null)); 

     navigationFragment = (NavigationFragment) 
       getSupportFragmentManager().findFragmentById(R.id.drawer_fragment); 
     navigationFragment.initDrawer(R.id.drawer_fragment, R.id.drawer_layout, toolbar); 

    } 
} 

这是我activity_drawer.xml

<?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> 

这是我OverviewActivity(我的启动/默认的活动)

public class OverviewActivity extends DrawerActivity { 
    OverviewFragment overview; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     createMenuDrawer(R.layout.activity_overview); 
    } 
} 

我的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" /> 

和......我的片段

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); 
    } 
} 

可有人给我解释一下,为什么容器是空?谢谢... :)

+0

据我所知,视图组是包含您的布局的视图。在很多情况下,它可以是空的并且没有什么大不了的。 您可以将参数附加到根改为** FALSE **例如:inflater.inflate(R.layout.fragment_overview,container,false); – kidnan1991 2014-12-05 00:54:41

回答

4

当使用静态方法时,您的<fragment>需要包含在视图组中。只需将RelativeLayout作为父文件放入XML文件即可。

+0

谢谢..你能解释我为什么必须把它放在RelativeLayout中吗? – Tobias 2014-12-08 00:44:24

+1

片段在实例化时需要父视图组。因此,当您静态使用它们时,它们必须包含在视图组(布局)中。你可以使用任何可用的布局,而不仅仅是'RelativeLayout'。 – 2014-12-08 00:58:02

+0

但DrawerLayout是一个ViewGroup,我不知道为什么是这个问题 – 2017-06-16 16:30:48