Android的碎片 - onCreateView - 容器是空 [英] Android Fragment - onCreateView - container is null

查看:142
本文介绍了Android的碎片 - onCreateView - 容器是空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题与onCreateView方法 - 给定的容器/父母为空,所以我不能夸大我的布局。这是我的code:

I have a small problem with the onCreateView Method - the given container/parent is null and therefore i can't inflate my layout. Here is my code:

这是我的主类

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

This is my 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(我的发射/默认的活动)

This is my OverviewActivity (my launcher/default activity)

public class OverviewActivity extends DrawerActivity {
    OverviewFragment overview;

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

        createMenuDrawer(R.layout.activity_overview);
    }
}

我activity_overview.xml

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

和...我片段

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

有人能解释我为什么容器是空?谢谢...:)

Can anybody explain me, why the container is null? Thanks... :)

推荐答案

&LT;片断&gt; 需要使用静态方法时,包含在一个视图组。只要把一个 RelativeLayout的,因为它的父XML文件中的

Your <fragment> needs to be contained in a view group when using the static approach. Just put a RelativeLayout as it's parent in the XML file.

这篇关于Android的碎片 - onCreateView - 容器是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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