-2
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.app.ActionBarDrawerToggle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends Activity {
    private DrawerLayout drawerLayout;
    private ListView lvSlidingMenu;
    private ActionBarDrawerToggle drawerToggle; // Navigation Drawer titles
    private CharSequence drawerTitle;
    private CharSequence appTitle;

    // Sliding Menu items
    private String[] titles;
    private TypedArray icons;
    private ArrayList slidingMenuItems;
    private SlidingMenuAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        appTitle = drawerTitle = getTitle();

        // Load resources
        titles = getResources().getStringArray(R.array.nav_drawer_items);
        icons = getResources().obtainTypedArray(R.array.nav_drawer_icons);

        // Get Sliding Menu ListView istance
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        lvSlidingMenu = (ListView) findViewById(R.id.lv_sliding_menu);
        slidingMenuItems = new ArrayList();

        // Creating and Adding SlidingMenuItems
        slidingMenuItems.add(new SlidingMenuItem(titles[0], icons.getResourceId(0,-1)));
        slidingMenuItems.add(new SlidingMenuItem(titles[1], icons.getResourceId(1, -1)));
        slidingMenuItems.add(new SlidingMenuItem(titles[2], icons.getResourceId(2, -1)));
        slidingMenuItems.add(new SlidingMenuItem(titles[3], icons.getResourceId(3, -1)));

        // Recycle the typed array
        icons.recycle();
        lvSlidingMenu.setOnItemClickListener(new SlideMenuClickListener());

        // Assign adapter to listview
        adapter = new SlidingMenuAdapter(getApplicationContext(), slidingMenuItems);
        lvSlidingMenu.setAdapter(adapter);

        // Enable action bar app icon and behaving it as toggle button
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
        drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer
                // Navigation Drawer
               , R.string.app_name,
                //Navigation Drawer open - description for accessibility
                R.string.app_name)
                // Navigation Drawer close - description for accessibility
         {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(appTitle);
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(drawerTitle);
                invalidateOptionsMenu();
            }
        }; drawerLayout.setDrawerListener(drawerToggle);
        if (savedInstanceState == null) {
            // On first time, show Home Fragment
            displayView(0);
        }
    }

    /**
     * Slide menu item click listener *
     */
    private class SlideMenuClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // Display appropriate fragment for selected item
            displayView(position);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Toggle Navigation Drawer on selecting action bar app icon/title
        if (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle action bar actions click
        switch (item.getItemId()) {
            case R.id.action_settings:
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    } /* * * Called when invalidateOptionsMenu() is triggered */

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If Navigation Drawer is opened, hide the action items
        boolean drawerOpen = drawerLayout.isDrawerOpen(lvSlidingMenu);
        menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    /**
     * Display fragment view for selected Navigation Drawer list item *
     */
    private void displayView(int position) {
        Fragment fragment = null;
        switch (position) {
            case 0:
                fragment = new HomeFragment();
                break;

            default:
                break;
        }
        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.fragment_detail, fragment).commit();

            // Update selected item and title, then close the drawer
            lvSlidingMenu.setItemChecked(position, true);
            lvSlidingMenu.setSelection(position);
            setTitle(titles[position]);
            drawerLayout.closeDrawer(lvSlidingMenu);
        } else {
            // Log error
            Log.e("MainActivity", "Error in creating fragment");
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        appTitle = title;
        getActionBar().setTitle(appTitle);
    }

    /**
     * When using the ActionBarDrawerToggle, you must call it during * onPostCreate() and onConfigurationChanged()...
     */
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        drawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        drawerToggle.onConfigurationChanged(newConfig);
    }
}

This is my MainActivty.java

public class SlidingMenuAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<SlidingMenuItem> items;

    public SlidingMenuAdapter(Context context, ArrayList<SlidingMenuItem> items) {
        this.context = context;
        this.items = items;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int index) {
        return items.get(index);
    }

    @Override
    public long getItemId(int index) {
        return index;
    }

    @Override
    public View getView(int index, View view, ViewGroup arg2) {
        if (view == null) {
            LayoutInflater mInflater = (LayoutInflater)
                    context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            view = mInflater.inflate(R.layout.iv_item_sliding_menu, null);
        }

        ImageView imgIcon = (ImageView) view.findViewById(R.id.img_sliding_menu_item);
        TextView txtTitle = (TextView) view.findViewById(R.id.tv_sliding_menu_item);

        SlidingMenuItem item = items.get(index);

        imgIcon.setImageResource(item.getIcon());
        txtTitle.setText(item.getTitle());

        return view;
    }

} 

This is my adapter.java file

public class SlidingMenuItem {
    String title;
    int icon;

    public SlidingMenuItem(String title, int icon) {
        this.title = title;
        this.icon = icon;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getIcon() {
        return icon;
    }

    public void setIcon(int icon) {
        this.icon = icon;
    }

}

And this is my menu item

<resources>
    <string name="app_name" translatable="false">Fb slid</string>

    <string name="hello_world" translatable="false">Hello world!</string>
    <string name="action_settings" translatable="false">Settings</string>
    <string name="title_activity_main" translatable="false">MainActivity
    </string>


        <!-- Sliding Menu items -->
 <string-array name="nav_drawer_items">
    <item >Home</item>
    <item >Notifications</item>
    <item >Settings</item>
    <item >About</item>
</string-array>

        <!-- Sliding Menu item icons -->
        <array name="nav_drawer_icons">
            <item>@drawable/home</item>
            <item>@drawable/notifications</item>
            <item>@drawable/settings</item>
            <item>@drawable/about</item>
        </array>

    </resources>

And this s my string.xml file

Log cat :

10-03 11:02:06.217    6956-6956/com.example.first.fbslid E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.first.fbslid/com.example.first.fbslid.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2146)
            at android.app.ActivityThread.access$700(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:177)
            at android.app.ActivityThread.main(ActivityThread.java:4947)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)

            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
            at com.example.first.fbslid.MainActivity.onCreate(MainActivity.java:49)
            at android.app.Activity.performCreate(Activity.java:5207)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2085)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2146)
            at android.app.ActivityThread.access$700(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:177)
            at android.app.ActivityThread.main(ActivityThread.java:4947)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method).

I don't know where am getting error as am new to android and am trying to create sliding menu like in facebook . Can someone Please help me to resolve

6
  • Which is 49th line in MainActivity? Commented Oct 3, 2015 at 5:46
  • slidingMenuItems.add(new SlidingMenuItem(titles[0], icons.getResourceId(0,-1))); Commented Oct 3, 2015 at 5:47
  • You Haven't defined nav_drawer_items in Your String File.. Commented Oct 3, 2015 at 5:50
  • before i have defined but it throws duplicate resource value Commented Oct 3, 2015 at 5:52
  • give different name and try again.. its because you have used that name somewhere else. Commented Oct 3, 2015 at 5:55

1 Answer 1

1

I think is the

<!-- Sliding Menu items -->

that its empty, and then when you try to read the menu item array it fails

   titles = getResources().getStringArray(R.array.nav_drawer_items);
Sign up to request clarification or add additional context in comments.

3 Comments

I would define as an array the items name, and then , define the icons for the items on the code Array of strings: stackoverflow.com/questions/5013632/… Array of drawables: stackoverflow.com/questions/3355220/…
somebody please help me out am trying this thing for past 1 week please post your updated code
I think the problem is trying to define an array of drawables on a strings resource. Define there just the array of item names, and define on the java class which drawable corresponds to each item

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.