I'm trying to implement a navigation drawer (based on this tutorial).
When an item in the drawer is clicked, I want to open a fragment. Here's some code from my MainActivity.java
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int editedPosition = position + 1;
Toast.makeText(MainActivity.this, "You selected item " + editedPosition, Toast.LENGTH_SHORT).show();
switch(position){
case 0:
Log.d("SWITCH", "Case 0");
if (!(getSupportFragmentManager().getFragments().get(0) instanceof NewLogFragment)) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.contentFrame, Fragment
.instantiate(MainActivity.this, Fragments.ONE.getFragment()))
.commit();
}
break;
case 1:
if (!(getSupportFragmentManager().getFragments().get(0) instanceof LogFragment)) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.contentFrame, Fragment
.instantiate(MainActivity.this, Fragments.ONE.getFragment()))
.commit();
}
break;
}
mDrawerLayout.closeDrawer(mDrawerList);
}
});
Here's my activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/primary"
app:popupTheme="@style/Theme.AppCompat"
app:theme="@style/ToolbarTheme" />
<!-- Main layout -->
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/contentFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
<!-- Nav drawer -->
<ListView
android:id="@android:id/list"
android:layout_width="305dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
Also, here's my Fragments.java
public enum Fragments {
ONE(NewLogFragment.class),TWO(LogFragment.class);
final Class<? extends Fragment> fragment;
private Fragments(Class<? extends Fragment> fragment) {
this.fragment = fragment;
}
public String getFragment() {
return fragment.getName();
}}
When I run this, I get
java.lang.NullPointerException at sleeping_vityaz.fivethreeone_trainer.MainActivity$1.onItemClick(MainActivity.java:117)
Line 111 is this line
`if (!(getSupportFragmentManager().getFragments().get(0) instanceof LogFragment)) {`
More details:
- MainActivity extends ActionBarActivity
- My fragments extends
Fragment
(android.support.v4.app.Fragment) everywhere
So why am I getting this null pointer exception?
EDIT I've applied Rohit's suggestion and no longer get any errors. However the layout doesn't change. What could be wrong?
EDIT@ I figured it out... wasn't adding the fragments to the activity in the first place... so there was nothing to replace duh :) Thanks everyone!