我有一個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);
}
}
可有人給我解釋一下,爲什麼容器是空?謝謝... :)
據我所知,視圖組是包含您的佈局的視圖。在很多情況下,它可以是空的並且沒有什麼大不了的。 您可以將參數附加到根改爲** FALSE **例如:inflater.inflate(R.layout.fragment_overview,container,false); – kidnan1991 2014-12-05 00:54:41