如何在Android

时间:2016-10-28 16:55:06

标签: java android android-library android-menu

我想在我的应用程序中使用此库作为菜单:GuillotineMenu-Android

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

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

并将此代码设置为构建器:

        .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)

我认为我应该在onCreate()方法中初始化 mGuillotineAnimation ,但我该如何初始化呢?

全部谢谢< 3

1 个答案:

答案 0 :(得分:2)

您忘记了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();