0

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!

naja
  • 571
  • 1
  • 4
  • 13
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Simon Jan 09 '15 at 19:54

2 Answers2

0

Your problem is getFragments() method which returns null pointer. If You look into Android API documentation, there is no single word about that method. And if something is not mentioned in official API, don't expect that it will work xD

EDIT: And in second case You want to have:

instantiate(MainActivity.this, Fragments.TWO.getFragment()))

instead of

instantiate(MainActivity.this, Fragments.ONE.getFragment()))

EDIT 2 Well, lets try with switch like that:

 switch(position){
                        case 0:
                            Log.d("SWITCH", "Case 0");  
                                getSupportFragmentManager().beginTransaction()
                                        .replace(R.id.contentFrame, Fragment
                                                .instantiate(MainActivity.this, Fragments.ONE.getFragment()))
                                        .commit();
                            break;
                        case 1:
                                getSupportFragmentManager().beginTransaction()
                                        .replace(R.id.contentFrame, Fragment
                                                .instantiate(MainActivity.this, Fragments.TWO.getFragment()))
                                        .commit();
                            break;
                    }
Wiertek
  • 368
  • 1
  • 8
  • thanks. Rohit's suggestion got rid of the null pointer exception but now my layout doesn't change onItemClick. Any ideas? – naja Jan 09 '15 at 20:00
  • :( removing the if statements didn't help either – naja Jan 09 '15 at 20:12
-1

Replace

if (!(getSupportFragmentManager().getFragments().get(0) instanceof NewLogFragment))

with

if (!(getSupportFragmentManager().findFragmentById(R.id.contentFrame) instanceof NewLogFragment))

Similarly change case 1 too

EDIT

Replace

Fragments.ONE.getFragment()

with

Fragments.TWO.getFragment()

in case 0

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57