Reputation: 5463
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:
This is my "main" class
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);
}
}
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>
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);
}
}
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" />
and... my fragment
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... :)
Upvotes: 4
Views: 6915
Reputation: 15775
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.
Upvotes: 4