1

I am trying to set up a listview for some actions that is displayed on a Navigation Drawer that opens from the right. The project will compile with no errors and I tried out my BaseAdapter on a ListView that is already in view and that works fine. There is no action bar in my app so the user has to open the drawer by sliding from the right edge of the screen. All the info to be displayed in the listview is handled in the adapter.

Here is what is in onCreate that is related to the drawer (On the official tutorial it seemed the only reason they would reference the actual DrawerLayout is to give it a toggle on the action bar, which I don't have):

I am now getting a null pointer exception when I try to assign the adapter. ListView drawerList = (ListView) findViewById(R.id.drawerList); returns null

UPDATED CODE

    ListView drawerList = (ListView) findViewById(R.id.drawerList);
    SectionedAdapter DrawerAdapter = new SectionedAdapter(this);
    drawerList.setAdapter(DrawerAdapter);

main_activity.xml

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

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

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


        <!--Always returns null-->
        <ListView android:id="@+id/drawerList"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:background="#ff1f1f1f"
            android:drawSelectorOnTop="true"/>

</android.support.v4.widget.DrawerLayout>
user1971
  • 698
  • 2
  • 6
  • 18
  • If I keep the file drawer xml file alone and do not inflate then I get a null pointer anytime I try to do anything with the ListView in the drawer (like setting the adapter) – user1971 Aug 16 '14 at 04:15

3 Answers3

1

Your xml seems to be fine but y r u inflating the menu_drawer.xml in fragment and not on an Activity ..Check out this video for better idea :: https://www.youtube.com/watch?v=K8hSIP2ha-g

this is my code..

activity_main.xml

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawerlayout">
    <FrameLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/maincontent"></FrameLayout>
    <ListView 
        android:background="#FFFFFF"
        android:divider="@null"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:id="@+id/drawerlist"
        android:layout_gravity="left">
    </ListView>
</android.support.v4.widget.DrawerLayout>

MainActivity.java

package com.example.nav;

import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
   DrawerLayout mdrawer;
   private ListView list;
   private String[] planets;
   ActionBar action;
   ActionBarDrawerToggle mtoggle;
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list=(ListView)findViewById(R.id.drawerlist);
        mdrawer=(DrawerLayout)findViewById(R.id.drawerlayout);
        action=getActionBar();
        mtoggle=new ActionBarDrawerToggle(this, mdrawer, R.drawable.ic_drawer,R.string.OndrawerOpen,R.string.OndrawerClose){

            public void onDrawerClosed(View drawerView) {

                super.onDrawerClosed(drawerView);
            }
            @Override
            public void onDrawerOpened(View drawerView) {

                super.onDrawerOpened(drawerView);
            }
            @Override
            public boolean onOptionsItemSelected(MenuItem item) {

                return super.onOptionsItemSelected(item);
            }
        };
        mdrawer.setDrawerListener(mtoggle);
        getActionBar().setHomeButtonEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        planets=getResources().getStringArray(R.array.planets);
        list.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,planets));
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
                setTitle(planets[position]);
            }
        });
   }
        @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                if(mtoggle.onOptionsItemSelected(item)){
                    return true;
                }
                return super.onOptionsItemSelected(item);
            }
        @Override
            protected void onPostCreate(Bundle savedInstanceState) {
                super.onPostCreate(savedInstanceState);
                mtoggle.syncState();
            }
        void setTitle(String value){
            getActionBar().setTitle(value);
        }
}

This might help!!

DRY Believer
  • 1,001
  • 11
  • 20
  • Do I have to do anything with the action bar? I just want a slide gesture. The action bar is not in my app. Also I am inflating because it appears as though the xml file that has the drawer is in a separate file from the main activity. Do I have to include that in with the rest of my layout or does the drawer stand alone. – user1971 Aug 16 '14 at 04:08
  • refer to http://stackoverflow.com/questions/20971245/navigation-drawer-without-actionbar… ..the drawer stands alone u dont need to inflate it .... please refer to the code and video above... and also when we setcontentView(YourXMLpath containing the supportlibrary for navigation drawer) automatically it include the navigation drawer....i hope this helps – DRY Believer Aug 16 '14 at 04:42
  • 1
    to add there is no relationship between navigationdrawer and actionbar – DRY Believer Aug 16 '14 at 04:44
  • Wow, I don't know why I didn't see it before. So My activity xml should look like `` – user1971 Aug 16 '14 at 04:54
  • If you can tell me why findViewById(R.id.drawList); always returns null I think it will work now. – user1971 Aug 16 '14 at 05:50
  • That problem seemed to be caused by another layout folder being created some how when I imported the support library. My listview wasn't in that file. – user1971 Aug 17 '14 at 14:19
0

I have a solution for your problem:

1/ It should set a

android:theme="@style/AppThemeNoBar"

style in your manifest. Add style

<style name="AppThemeNoBar" parent="Theme.AppCompat.Light">
     <item name="android:windowNoTitle">true</item>
</style>

2/ menu_drawer :

<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:id="@+id/content_frame"
     android:layout_width="match_parent"
     android:layout_height="match_parent" >
 </RelativeLayout>
 <!-- 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>

3/ MainActivity:

DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(
            this, mDrawerLayout, R.drawable.menu_icon, R.string.drawer_open,
            R.string.drawer_close
            ) {
        @Override
        public void onDrawerClosed(View view) {
            invalidateOptionsMenu(); 
        }
        @Override
        public void onDrawerOpened(View drawerView) {
            invalidateOptionsMenu();                
        }
};

As a result you get a swipe-menu without ActionBar.

Mykhailo
  • 331
  • 3
  • 18
0

Maybe you made a mistake in view id name:
findViewById(R.id.drawList);

it should be R.id.drawerList instead of R.id.drawList.

P.S. I don't have enough rating to comment on answer.

Mykhailo
  • 331
  • 3
  • 18