javaandroidnullpointerexceptionactionbardrawertoggle

ActionBarDrawerToggle NullPointerException while loading from resources


When I launch the app, there are some issues with referencing the string values, They are navigation_drawer_open and navigation_drawer_close. Moreover, the problem occurred in the ActionBarDrawerToggle as well as drawer.addDrawerListener syntaxes.

         @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.input);

                Toolbar toolbar1 = findViewById(R.id.toolbar);
                setSupportActionBar(toolbar1);

                drawer=findViewById(R.id.drawer_layout);

                final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar1,
                        R.string.navigation_drawer_open,
                        R.string.navigation_drawer_close);
                drawer.addDrawerListener(toggle);

                toggle.syncState();

            }

            @Override
            public void onBackPressed() {
                if(drawer.isDrawerOpen(GravityCompat.START)) {
                    drawer.closeDrawer(GravityCompat.START);
                } else
                {
                    super.onBackPressed();
                }

            }

Due to I added the pages of nav_draw it cannot the main page (cause I have login and registration), so I created the first page of menu: input.xml The menu class referencing to it. layout: input.xml // The is no nothing, just empty layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello world" />
</LinearLayout>

And the last one is values. The syntax ActionBarDrawerToggle referencing to it.

    strings.xml: // the string values

    <resources>
        <string name="app_name">InformX</string>
        <string name="start">Start</string>
        <string name="title_activity_2">activity_2</string>
        <string name="todo">TODO</string>
        <string name="name">Name</string>
        <string name="first_name">First Name</string>
        <string name="title_activity_menu">menu</string>
        <string name="navigation_drawer_open">Open navigation drawer</string> //This one
        <string name="navigation_drawer_close">Close navigation drawer</string> // And this
        <string name="nav_header_title">Android Studio</string>
        <string name="nav_header_subtitle">android.studio@android.com</string>
        <string name="nav_header_desc">Navigation header</string>
        <string name="action_settings">Settings</string>

        <string name="menu_home">Home</string>
        <string name="menu_gallery">Gallery</string>
        <string name="menu_slideshow">Slideshow</string>
        <string name="menu_tools">Tools</string>
        <string name="menu_share">Share</string>
        <string name="menu_send">Send</string>
        <string name="tab_text_1">Tab 1</string>
        <string name="tab_text_2">Tab 2</string>
        <string name="hello_blank_fragment">Hello blank fragment</string>


        </resources>

logcat:

Error:  
    Process: com.example.myapp, PID: 31593
        java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myapp/com.example.myapp.menu}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
            at android.app.ActivityThread.-wrap12(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:154)
            at android.app.ActivityThread.main(ActivityThread.java:6077)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
         Caused by: java.lang.NullPointerException: Attempt to invoke virtual //The main problem method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
            at android.content.ContextWrapper.getResources(ContextWrapper.java:86)
            at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:127)
            at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:121)
            at androidx.appcompat.app.AppCompatActivity.getResources(AppCompatActivity.java:566)
            at com.example.myapp.menu.<init>(menu.java:13)
            at java.lang.Class.newInstance(Native Method)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
            at android.app.ActivityThread.-wrap12(ActivityThread.java) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:154) 
            at android.app.ActivityThread.main(ActivityThread.java:6077) 
            at java.lang.reflect.Method.invoke(Native Method) 
            atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

Solution

  • The activity call setContentView(R.layout.input) but into the input.xml the Toolbar with id="toolbar" is missing so findViewById(R.id.toolbar) fail. Also findViewById(R.id.drawer_layout) will fail because the DrawerLayout with id="drawer_layout" it is missing into the input.xml. You can try with a layout like this:

    <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"
        android:fitsSystemWindows="true">
    
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="#ff6d7fe2"
            android:minHeight="?attr/actionBarSize"
            app:contentInsetEnd="0dp"
            app:contentInsetLeft="0dp"
            app:contentInsetRight="0dp"
            app:contentInsetStart="0dp"
            />
    
    
            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Hello world" />
    
         </LinearLayout>
    
    </android.support.v4.widget.DrawerLayout>