Nate
Nate

Reputation: 412

Issue with extending BaseActivity for navigation drawer

I have looked around SO as well as Google searches, and have decided that my activities extending a BaseActivity was the best way to integrate a navigation drawer in my project. Whenever I start my app though, it crashes immediately. Below I have included my BaseActivity code and the issue lies with this line:

    drawerLayout.setDrawerListener(drawerToggle);

It results in a NullPointerException

public class BaseActivity extends Activity
{
    public DrawerLayout drawerLayout;
    public ListView drawerList;
    public String[] layers;
    private ActionBarDrawerToggle drawerToggle;
    Intent twitch = new Intent(this, TwitchActivity.class);
    Intent community = new Intent(this, CommunityActivity.class);
    Intent esports = new Intent(this, ESportsActivity.class);
    Intent home = new Intent(this, MainActivity.class);

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.id.drawer_layout);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        final ActionBar actionBar = getActionBar();

        drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout, R.drawable.ic_launcher, 0, 0)
        {
            public void onDrawerClosed(View view)
            {
                actionBar.setTitle(R.string.app_name);
            }

            public void onDrawerOpened(View drawerView)
            {
                actionBar.setTitle(R.string.menu);
            }
        };
        drawerLayout.setDrawerListener(drawerToggle);

        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);


        layers = getResources().getStringArray(R.array.layers);
        drawerList = (ListView) findViewById(R.id.left_drawer);
        drawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, android.R.id.text1,
                layers));


        drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
                String selected = arg0.getItemAtPosition(pos).toString();
                if(selected.equals("Twitch"))
                    startActivity(twitch);
                if(selected.equals("Community"))
                    startActivity(community);
                if(selected.equals("ESports"))
                    startActivity(esports);
                if(selected.equals("Home"))
                    startActivity(home);
            }
        });
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);

    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }

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

My MainActivity

public class MainActivity extends BaseActivity {

private Spinner spinner;
public static String region;
public static String name;

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

My drawer_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<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">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Nav 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="#111111"/>
</android.support.v4.widget.DrawerLayout>

Full Error:

04-10 20:17:25.409    4548-4548/edu.appdesign.leaguestats E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: edu.appdesign.leaguestats, PID: 4548
    java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.appdesign.leaguestats/edu.appdesign.leaguestats.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f08003d type #0x12 is not valid
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
            at android.app.ActivityThread.access$800(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
            at android.os.Handler.dispatchMessage(Handler.java)
            at android.os.Looper.loop(Looper.java)
            at android.app.ActivityThread.main(ActivityThread.java)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f08003d type #0x12 is not valid
            at android.content.res.Resources.loadXmlResourceParser(Resources.java)
            at android.content.res.Resources.getLayout(Resources.java)
            at android.view.LayoutInflater.inflate(LayoutInflater.java)
            at android.view.LayoutInflater.inflate(LayoutInflater.java)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java)
            at android.app.Activity.setContentView(Activity.java)
            at edu.appdesign.leaguestats.BaseActivity.onCreate(BaseActivity.java:34)
            at edu.appdesign.leaguestats.MainActivity.onCreate(MainActivity.java:42)
            at android.app.Activity.performCreate(Activity.java)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
            at android.app.ActivityThread.access$800(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
            at android.os.Handler.dispatchMessage(Handler.java)
            at android.os.Looper.loop(Looper.java)
            at android.app.ActivityThread.main(ActivityThread.java)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
            at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 834

Answers (1)

cybersam
cybersam

Reputation: 67009

Your onCreate() must call setContentView() to set the layout before it calls findViewById() to get a view from within that layout. And make sure that you pass to setContentView() the proper layout resource (it should be something like 'R.layout.xxxxx', where 'xxxxx' is the name of your XML layout file, without the '.xml' extension).

Upvotes: 3

Related Questions