0

I have a navigation Drawer in my App.Now i need to make that drawer appear in all my activities.

I saw many questions similar to this & found a Solution https://stackoverflow.com/a/4922740/2781359

But I tried it in my App.Then it throws an Exception.

So Help me in the Right Direction :)

Thanks for your Help ...
Logcat

       01-25 22:35:14.062: E/AndroidRuntime(17091): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ControllerDroid.client/com.ControllerDroid.client.activity.ControlActivity}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.app.ActivityThread.access$800(ActivityThread.java:145)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.os.Handler.dispatchMessage(Handler.java:102)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.os.Looper.loop(Looper.java:136)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.app.ActivityThread.main(ActivityThread.java:5081)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at java.lang.reflect.Method.invokeNative(Native Method)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at java.lang.reflect.Method.invoke(Method.java:515)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at dalvik.system.NativeStart.main(Native Method)
01-25 22:35:14.062: E/AndroidRuntime(17091): Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.widget.AdapterView.addView(AdapterView.java:478)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.view.LayoutInflater.inflate(LayoutInflater.java:500)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at com.ControllerDroid.client.activity.MainActivity.setContentView(MainActivity.java:37)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at com.ControllerDroid.client.activity.MainActivity.onCreate(MainActivity.java:56)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at com.ControllerDroid.client.activity.ControlActivity.onCreate(ControlActivity.java:62)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.app.Activity.performCreate(Activity.java:5231)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-25 22:35:14.062: E/AndroidRuntime(17091):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
01-25 22:35:14.062: E/AndroidRuntime(17091):    ... 11 more

MainActivity.java

    public class MainActivity extends ActionBarActivity
{
    protected DrawerLayout fullLayout;
    protected ListView actContent;
    private ListView mDrawerList;
    private DrawerLayout mDrawer;
    private CustomActionBarDrawerToggle mDrawerToggle;
    private String[] menuItems;
    @Override
    public void setContentView(final int layoutResID)
    {
       fullLayout = (DrawerLayout)getLayoutInflater().inflate(R.layout.activity_main_drawer, null); // Your  base layout here
        ListView actContent = (ListView) fullLayout.findViewById(R.id.drawer);
        getLayoutInflater().inflate(layoutResID, actContent, true); // Setting    the    content  of layout your  provided  to the  act_content frame
        super.setContentView(fullLayout);
        // here you can get your drawer buttons and define how they should
        // behave and what must they do, so you won't be needing to repeat it in
        // every activity class
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                getSupportActionBar().setHomeButtonEnabled(true);


                // set a custom shadow that overlays the main content when the drawer
                // opens
                fullLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

                _initMenu();
                mDrawerToggle = new CustomActionBarDrawerToggle(this, fullLayout);
                fullLayout.setDrawerListener(mDrawerToggle);
    }


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

activity_main_drawer.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
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/text_color"
            android:text="@string/drawer_text" />
    </RelativeLayout>

    <!-- The navigation drawer -->

    <ListView
        android:id="@+id/drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:cacheColorHint="#00000000" 
         android:background="@color/list_background"
        android:choiceMode="singleChoice"
        android:textAppearance="@android:style/TextAppearance.Medium"
         android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

</android.support.v4.widget.DrawerLayout>
Community
  • 1
  • 1
AruLNadhaN
  • 2,808
  • 5
  • 24
  • 42
  • Is the use of fragments not an alternative? If so I highly recommend you to use fragments as they're a lot more 'flexible'. – Endzeit Jan 25 '14 at 16:29
  • `java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView` did you find any solution for this error – Straw Hat Jan 28 '14 at 06:59

2 Answers2

1

Your MainActivity is abstract and cannot be instantiated.

If you intend to keep it abstract, remove its entry from the manifest and make sure you're not trying to instantiate with an explicit Intent.

laalto
  • 150,114
  • 66
  • 286
  • 303
0

You are casting the wrong layout id's

Try this

public abstract class MainActivity extends ActionBarActivity
{
    protected DrawerLayout fullLayout;
    protected ListView actContent;
    private ListView mDrawerList;
    private CustomActionBarDrawerToggle mDrawerToggle;
    private String[] menuItems;
    @Override
    public void setContentView(final int layoutResID)
    {
       fullLayout = (DrawerLayout)getLayoutInflater().inflate(R.layout.activity_main_drawer, null); // Your  base layout here
        ListView actContent = (ListView) fullLayout.findViewById(R.id.drawer);
        getLayoutInflater().inflate(layoutResID, actContent, true); // Setting    the    content  of layout your  provided  to the  act_content frame
        super.setContentView(fullLayout);
        // here you can get your drawer buttons and define how they should
        // behave and what must they do, so you won't be needing to repeat it in
        // every activity class
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                getSupportActionBar().setHomeButtonEnabled(true);


                // set a custom shadow that overlays the main content when the drawer
                // opens
                fullLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

                _initMenu();
                mDrawerToggle = new CustomActionBarDrawerToggle(this, fullLayout);
                fullLayout.setDrawerListener(mDrawerToggle);
    }

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

Edited Full Solution :

main_content.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/act_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <RelativeLayout
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start">

        <ListView
            android:layout_below="@+id/drawer_list"
            android:layout_width="240dp"
            android:id="@+id/filters_list"
            android:layout_height="wrap_content"/>
    </RelativeLayout>       
</android.support.v4.widget.DrawerLayout>

BaseActivity

protected DrawerLayout fullLayout;
protected FrameLayout actContent;
private ActionBarDrawerToggle mDrawerToggle;
ListView mDrawerList;


@Override
public void setContentView(final int layoutResID) {


    fullLayout= (DrawerLayout) getLayoutInflater().inflate(R.layout.main_content, null); // Your base layout here


    mDrawerList = (ListView)fullLayout.findViewById(R.id.drawer_list);
    fullLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,mFilterList);
    fullLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

    int drawer_icon = R.drawable.ic_drawer;   
    mDrawerToggle = new ActionBarDrawerToggle(
            this,
            fullLayout,
           drawer_icon,
            R.string.drawer_open,
         R.string.drawer_close
        ) {
        public void onDrawerClosed(View view) {
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };
    fullLayout.setDrawerListener(mDrawerToggle);

    actContent= (FrameLayout) fullLayout.findViewById(R.id.act_content);
    getLayoutInflater().inflate(layoutResID, actContent, true); // Setting the content of layout your provided to the act_content frame

    super.setContentView(fullLayout);
}

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

Thanks

eshbeata
  • 306
  • 2
  • 6
  • @ArulNadhan just edited the answer with full working solution – eshbeata Jan 25 '14 at 17:11
  • Shows this Exception java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ControllerDroid.client/com.ControllerDroid.client.activity.MainActivity}: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.support.v4.widget.DrawerLayout – AruLNadhaN Jan 25 '14 at 17:43
  • @ArulNadhan did you use the same layout file ? – eshbeata Jan 25 '14 at 20:31
  • I used both your xml & Class file Only . – AruLNadhaN Jan 26 '14 at 02:07