如何在Android中初始化菜单库



我想在我的应用程序中使用这个库的菜单:GuillotineMenu-Android.

对于带有onBackPress按钮的关闭菜单,我使用以下代码:

@Override
public void onBackPressed() {
    if (!isOpened) {
        super.onBackPressed();
    }
    mGuillotineAnimation.close();
}

并将此代码设置为builder:

        .setGuillotineListener(new GuillotineListener() {
            @Override
            public void onGuillotineOpened() {
                isOpened = true;
            }
            @Override
            public void onGuillotineClosed() {
                isOpened = false;
            }
        })

Builder完整代码:

View guillotineMenu = LayoutInflater.from(this).inflate(R.layout.menu_layout, null);
root.addView(guillotineMenu);
toolbar_menuImage = (ImageView) toolbar.findViewById(R.id.toolbar_pages_logo);
new GuillotineAnimation.GuillotineBuilder(guillotineMenu, guillotineMenu.findViewById(R.id.menu_layout_image), toolbar_menuImage)
        .setStartDelay(RIPPLE_DURATION)
        .setActionBarViewForAnimation(toolbar)
        .setClosedOnStart(true)
        .setGuillotineListener(new GuillotineListener() {
            @Override
            public void onGuillotineOpened() {
                isOpened = true;
            }
            @Override
            public void onGuillotineClosed() {
                isOpened = false;
            }
        })
        .build();

和onCreate()方法的下面代码:

private GuillotineAnimation mGuillotineAnimation;
private boolean isOpened = true;

但是当运行应用程序时,点击onBackPress按钮显示这个错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.app, PID: 28891
                  java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.app.Components.SlidingMenu.animation.GuillotineAnimation.close()' on a null object reference
                      at com.example.app.Activities.Category_page.onBackPressed(Category_page.java:256)
                      at android.app.Activity.onKeyUp(Activity.java:2506)
                      at android.view.KeyEvent.dispatch(KeyEvent.java:2633)
                      at android.app.Activity.dispatchKeyEvent(Activity.java:2757)
                      at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:543)
                      at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:53)
                      at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:312)
                      at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:53)
                      at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2297)
                      at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3995)
                      at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3957)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3488)
                      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3541)
                      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3507)
                      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3515)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3488)
                      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3541)
                      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3507)
                      at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3617)
                      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3515)
                      at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3674)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3488)
                      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3541)
                      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3507)
                      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3515)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3488)
                      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3541)
                      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3507)
                      at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3650)
                      at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3811)
                      at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2242)
                      at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1883)
                      at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1874)
                      at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2219)
                      at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
                      at android.os.MessageQueue.nativePollOnce(Native Method)
                      at android.os.MessageQueue.next(MessageQueue.java:143)
                      at android.os.Looper.loop(Looper.java:122)
                      at android.app.ActivityThread.main(ActivityThread.java:5349)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

我认为我应该初始化mGuillotineAnimationonCreate()方法,但我怎么能初始化这个?

谢谢大家<3

您忘记实例化mGuillotineAnimation:

 mGuillotineAnimation = new GuillotineAnimation.GuillotineBuilder(guillotineMenu, guillotineMenu.findViewById(R.id.menu_layout_image), toolbar_menuImage)
        .setStartDelay(RIPPLE_DURATION)
        .setActionBarViewForAnimation(toolbar)
        .setClosedOnStart(true)
        .setGuillotineListener(new GuillotineListener() {
            @Override
            public void onGuillotineOpened() {
                isOpened = true;
            }
            @Override
            public void onGuillotineClosed() {
                isOpened = false;
            }
        })
        .build();

相关内容

  • 没有找到相关文章

最新更新