0

I am trying to let the user open my navigation drawer by pressing my app icon in the navigation drawer. But when I open the Activity where I try it, my LogCat is giving me a NullPointerException in line 55. This is my code:

AddActivity.java:

package de.hoffmann.pod2go;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.NavUtils;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class AddActivity extends Activity {

    public ActionBarDrawerToggle mDrawerToggle;

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

        setContentView(R.layout.activity_add);
        // Show the Up button in the action bar.

        DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout, /* DrawerLayout object */
               R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_close  /* "close drawer" description */
                ) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(R.string.title_activity_add);
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(R.string.drawer_title);
            }
        };


        mDrawerLayout.setDrawerListener(mDrawerToggle);
        getActionBar().setDisplayHomeAsUpEnabled(true); // Pressing the app icon in the action bar will navigate to the parent activity.
        getActionBar().setHomeButtonEnabled(true);

    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
         super.onPostCreate(savedInstanceState);
         // Sync the toggle state after onRestoreInstanceState has occurred.
         mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
         // Pass the event to ActionBarDrawerToggle, if it returns
         // true, then it has handled the app icon touch event
         if (mDrawerToggle.onOptionsItemSelected(item)) {
             return true;
         }


         switch (item.getItemId()) 
            {
                case android.R.id.home:
                    NavUtils.navigateUpFromSameTask(this);
                    return true;
            }

         return super.onOptionsItemSelected(item);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.add, menu);
        return true;
    }
}

activty_add.xml:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The main content view -->

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/add_activity_feed_heading"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/add_feed_url"
            android:layout_alignParentRight="true" />

        <EditText
            android:id="@+id/feed_heading"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="14dp"
            android:ems="10"
            android:hint="@string/url_hint"
            android:layout_alignParentLeft="true"  />            

        <!--  Button bar -->
        <LinearLayout android:id="@+id/footer" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:orientation="horizontal"
            android:layout_alignParentBottom="true" >
            <Button
                style="@style/buttonBarButton"
                android:text="@string/buttonbar_left"
                android:onClick="goBack"/>
            <Button
                style="@style/buttonBarButton"
                android:text="@string/buttonbar_right"/>
        </LinearLayout>

    </RelativeLayout>

    <!-- Navigation Drawer -->
    <ListView android:id="@+id/left_drawer"
        style="@style/NavigationDrawer"/>

</android.support.v4.widget.DrawerLayout>

Also, do I have to paste all that code into every single activity file when it works?

1
  • so sorry, i wanted to add that but i forgot it! Line 55 is the last line in the onPostCreate method: mDrawerToggle.syncState(); Commented Nov 11, 2013 at 18:42

1 Answer 1

1

I guess problem lie in line 25 ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(

You just created a local variable with the same name with a global var mDrawerToggle remove the first ActionBarDrawerToggle in line 25 may make your code work

Hope this help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.