Sliding Menu example not working : java.lang.Illeg

2019-06-04 20:26发布

问题:

I downloaded Sliding Menu library and Actionsbarsherlock from Links, I found out example of it from example, which had an YouTube video link to show the working. I followed all the steps, but when I am running the code, its giving me.

 05-28 15:41:21.665: E/AndroidRuntime(2023): FATAL EXCEPTION: main
05-28 15:41:21.665: E/AndroidRuntime(2023): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rufflez.slidingmenuexample/com.rufflez.slidingmenuexample.MainActivity}: java.lang.IllegalStateException: RandomList must be attached to a SherlockFragmentActivity.
05-28 15:41:21.665: E/AndroidRuntime(2023):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-28 15:41:21.665: E/AndroidRuntime(2023):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-28 15:41:21.665: E/AndroidRuntime(2023):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-28 15:41:21.665: E/AndroidRuntime(2023):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-28 15:41:21.665: E/AndroidRuntime(2023):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-28 15:41:21.665: E/AndroidRuntime(2023):     at android.os.Looper.loop(Looper.java:137)
05-28 15:41:21.665: E/AndroidRuntime(2023):     at android.app.ActivityThread.main(ActivityThread.java:4424)
05-28 15:41:21.665: E/AndroidRuntime(2023):     at java.lang.reflect.Method.invokeNative(Native Method)
05-28 15:41:21.665: E/AndroidRuntime(2023):     at java.lang.reflect.Method.invoke(Method.java:511)
05-28 15:41:21.665: E/AndroidRuntime(2023):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
05-28 15:41:21.665: E/AndroidRuntime(2023):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
05-28 15:41:21.665: E/AndroidRuntime(2023):     at dalvik.system.NativeStart.main(Native Method)
05-28 15:41:21.665: E/AndroidRuntime(2023): Caused by: java.lang.IllegalStateException: RandomList must be attached to a SherlockFragmentActivity.
05-28 15:41:21.665: E/AndroidRuntime(2023):     at com.actionbarsherlock.app.SherlockListFragment.onAttach(SherlockListFragment.java:25)
05-28 15:41:21.665: E/AndroidRuntime(2023):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:867)
05-28 15:41:21.665: E/AndroidRuntime(2023):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
05-28 15:41:21.665: E/AndroidRuntime(2023):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-28 15:41:21.665: E/AndroidRuntime(2023):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
05-28 15:41:21.665: E/AndroidRuntime(2023):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
05-28 15:41:21.665: E/AndroidRuntime(2023):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
05-28 15:41:21.665: E/AndroidRuntime(2023):     at android.app.Activity.performStart(Activity.java:4475)
05-28 15:41:21.665: E/AndroidRuntime(2023):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1929)
05-28 15:41:21.665: E/AndroidRuntime(2023):     ... 11 more

No idea why the error is coming, I have add both libraries also.

回答1:

Issue:

java.lang.IllegalStateException: RandomList must be attached to a SherlockFragmentActivity

Solution:

you need to extend SherlockActivity

Reference:

Have a look at SlidingMenu usage guide ,

read last point of Step 2 inside section of How to Integrate this Library into Your Projects

"If you want to use another library such as ActionBarSherlock, you can just change the SlidingActivities to extend the SherlockActivities instead of the regular Activities"

Explaination:

As shown in above screenshot,

SlidingActivity extends SherlockActivity.

Similarly following changes are required,

 - SlidingListActivity extends SherlockListActivity
 - SlidingFragmentActivity extends SherlockFragmentActivity
 - SlidingPreferenceActivity extends SherlockPreferenceActivity

Note: Don't forget to add actionbarsherlock library to your SlidingMenu's library to access SherlockActivity.

I hope it will be helpful !!



回答2:

I solved it in two steps:

  1. Add actionbarsherlock library to SlidingMenu library as well.
  2. Open com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity and extend it from SherlockFragmentActivity .