-3

I want to implement Left and Right Side Sliding Menu in my application so I downloaded Project from GitUb.

Source : SlidingMenu

I have also added ABS as library, Then I was getting error The method getSupportActionBar() is undefined for the type BaseActivity in BaseActivity extends SlidingFragmentActivity so I replace extends SlidingFragmentActivity to extends SherlockFragmentActivity implements SlidingActivityBase so that error gone.

After this, I run Project and it saw error NullPointerException in LeftAndRightActivity.java.

Error is on line : super.onCreate(savedInstanceState)

How many changes I will need to do in this project ? and any Sample Running Project as per my requirement ?

Logcat:

05-23 11:53:32.417: E/AndroidRuntime(345): FATAL EXCEPTION: main
05-23 11:53:32.417: E/AndroidRuntime(345): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jeremyfeinstein.slidingmenu.example/com.jeremyfeinstein.slidingmenu.example.LeftAndRightActivity}: java.lang.NullPointerException
05-23 11:53:32.417: E/AndroidRuntime(345):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-23 11:53:32.417: E/AndroidRuntime(345):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-23 11:53:32.417: E/AndroidRuntime(345):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-23 11:53:32.417: E/AndroidRuntime(345):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-23 11:53:32.417: E/AndroidRuntime(345):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 11:53:32.417: E/AndroidRuntime(345):  at android.os.Looper.loop(Looper.java:123)
05-23 11:53:32.417: E/AndroidRuntime(345):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-23 11:53:32.417: E/AndroidRuntime(345):  at java.lang.reflect.Method.invokeNative(Native Method)
05-23 11:53:32.417: E/AndroidRuntime(345):  at java.lang.reflect.Method.invoke(Method.java:507)
05-23 11:53:32.417: E/AndroidRuntime(345):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-23 11:53:32.417: E/AndroidRuntime(345):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-23 11:53:32.417: E/AndroidRuntime(345):  at dalvik.system.NativeStart.main(Native Method)
05-23 11:53:32.417: E/AndroidRuntime(345): Caused by: java.lang.NullPointerException
05-23 11:53:32.417: E/AndroidRuntime(345):  at com.jeremyfeinstein.slidingmenu.example.BaseActivity.onCreate(BaseActivity.java:51)
05-23 11:53:32.417: E/AndroidRuntime(345):  at com.jeremyfeinstein.slidingmenu.example.LeftAndRightActivity.onCreate(LeftAndRightActivity.java:22)
05-23 11:53:32.417: E/AndroidRuntime(345):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-23 11:53:32.417: E/AndroidRuntime(345):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-23 11:53:32.417: E/AndroidRuntime(345):  ... 11 more

LeftandRightActivity.java:

public class LeftAndRightActivity extends BaseActivity {

    public LeftAndRightActivity() {
        super(R.string.left_and_right);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSlidingMenu().setMode(SlidingMenu.LEFT_RIGHT);
        getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

        setContentView(R.layout.content_frame);
        getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.content_frame, new SampleListFragment())
        .commit();

        getSlidingMenu().setSecondaryMenu(R.layout.menu_frame_two);
        getSlidingMenu().setSecondaryShadowDrawable(R.drawable.shadowright);
        getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.menu_frame_two, new SampleListFragment())
        .commit();
    }

}

BaseActivity.java:

public class BaseActivity extends SlidingFragmentActivity {

    private int mTitleRes;
    protected ListFragment mFrag;

    public BaseActivity(int titleRes) {
        mTitleRes = titleRes;
    }

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

        setTitle(mTitleRes);

        // set the Behind View
        setBehindContentView(R.layout.menu_frame);
        if (savedInstanceState == null) {
            FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
            mFrag = new SampleListFragment();
            t.replace(R.id.menu_frame, mFrag);
            t.commit();
        } else {
            mFrag = (ListFragment)this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
        }

        // customize the SlidingMenu
        SlidingMenu sm = getSlidingMenu();
        sm.setShadowWidthRes(R.dimen.shadow_width);
        sm.setShadowDrawable(R.drawable.shadow);
        sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        sm.setFadeDegree(0.35f);
        sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            toggle();
            return true;
        case R.id.github:
            Util.goToGitHub(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
Jeeten Parmar
  • 5,568
  • 15
  • 62
  • 111

1 Answers1

0

If you want to add LEFT and RIGHT both side sliding menu then just add

mSlidingMenu.setMode(SlidingMenu.LEFT_RIGHT);

if you want only LEFT slider then add

mSlidingMenu.setMode(SlidingMenu.LEFT);

and for right side slider add

mSlidingMenu.setMode(SlidingMenu.RIGHT);

where mSlidingMenu is the object of library so declare it like this SlidingMenu mSlidingMenu;

that's it, now you can slide from both sides.

user3660803
  • 315
  • 1
  • 3
  • 15