滑动菜单示例不起作用:java.lang.IllegalStateException:必须将RandomList附加到SherlockFragmentActivity

时间:2013-05-28 10:51:02

标签: android exception sliding slidingmenu

我从Links下载了滑动菜单库和Actionsbarsherlock,我从example找到了它的示例,其中有一个YouTube视频link来显示正常工作。我遵循了所有步骤,但是当我运行代码时,它给了我。

 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

不知道为什么会出现错误,我也添加了两个库。

libraries added

2 个答案:

答案 0 :(得分:2)

<强>问题:

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

<强>解决方案:

你需要扩展SherlockActivity

<强>参考:

查看SlidingMenu usage guide

阅读如何将此库集成到项目中

中的第2步的最后一点

“如果你想使用另一个库,比如ActionBarSherlock,你可以改变SlidingActivities来扩展SherlockActivities而不是常规活动”

<强>阐释:

enter image description here

如上图所示,

SlidingActivity extends SherlockActivity

同样需要进行以下更改,

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

注意: 不要忘记将actionbarsherlock库添加到SlidingMenu的库中以访问SherlockActivity

我希望它会有所帮助!!

答案 1 :(得分:0)

我分两步解决了这个问题:

  1. 同时将actionbarsherlock库添加到SlidingMenu库。
  2. 打开com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity并将其从SherlockFragmentActivity延伸