Java – findViewById(R.id.drawer_layout) returns null

findViewById(R.id.drawer_layout) returns null… here is a solution to the problem.

findViewById(R.id.drawer_layout) returns null

I have a question about the following line:

mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);

Returns a null value.

For debugging purposes, I tried the XML file found in the official Android DrawerLayout tutorial:

<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 -->
<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

This is the top of my FragmentActivity file:

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class PuzzleAllActivity extends FragmentActivity {

private ProgressDialog dialog;

private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_puzzle_all);

mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    if(mDrawerLayout == null){
        Log.d("DEBUG", "Drawer layout is null");
    }
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.menu_icon, R.string.drawer_open,
            R.string.drawer_close) {

public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES. GINGERBREAD) {
                invalidateOptionsMenu();
            }
        }

public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES. GINGERBREAD) {
                invalidateOptionsMenu();
            }
        }
    };

mDrawerLayout.setDrawerListener(mDrawerToggle);

For some reason, mDrawerLayout is null, which gives java.lang.NullPointerException:: for the last line

mDrawerLayout.setDrawerListener(mDrawerToggle); 

I’m new to Android programming and don’t know what is causing this problem.

I’ve made sure that the support library in the project’s build path is selected.

Here’s the error I got :

02-15 22:12:03.712: E/AndroidRuntime(17259): FATAL EXCEPTION: main
02-15 22:12:03.712: E/AndroidRuntime(17259): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.puzzleall.puzzleall/ com.puzzleall.puzzleall.PuzzleAllActivity}: java.lang.NullPointerException
02-15 22:12:03.712: E/AndroidRuntime(17259):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at android.app.ActivityThread.access$700(ActivityThread.java:150)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at android.os.Looper.loop(Looper.java:176)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at android.app.ActivityThread.main(ActivityThread.java:5279)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at java.lang.reflect.Method.invokeNative(Native Method)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at java.lang.reflect.Method.invoke(Method.java:511)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at dalvik.system.NativeStart.main(Native Method)
02-15 22:12:03.712: E/AndroidRuntime(17259): Caused by: java.lang.NullPointerException
02-15 22:12:03.712: E/AndroidRuntime(17259):    at com.puzzleall.puzzleall.PuzzleAllActivity.onCreate(PuzzleAllActivity.java:71)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at android.app.Activity.performCreate(Activity.java:5267)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
02-15 22:12:03.712: E/AndroidRuntime(17259):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)

I’m grateful for any help I can get 🙂

Solution

The NullPointer exception is caused by different layout files (“layout” and “layout-land”).

The Landscape version has an old layout file without DrawerLayout. So when it bloated, I got a NullPointer exception when referencing DrawerLayout.

Related Problems and Solutions