在BottomDialogFragment中,我使Draggble为false,但它得到警告消息

时间:2019-03-12 05:13:23

标签: java android

当我打开bottomDialogFragemnt时,它正处于警告之下,并且当我们在BottomDialogFragment上向右或向左滑动滚动条并且在android studio的Logcat中没有其他信息详细信息时,它会被拖动并关闭。

java.lang.NoSuchFieldException: No field mBehavior in class 
    Landroid/support/design/widget/BottomSheetDialog; (declaration of 
   'android.support.design.widget.BottomSheetDialog' )
System.err:     at java.lang.Class.getDeclaredField(Native Method)
System.err:     at android.support.v4.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:330)
System.err:     at android.support.v4.app.Fragment.performGetLayoutInflater(Fragment.java:1308)
System.err:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
System.err:     at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
System.err:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
System.err:     at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
System.err:     at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
System.err:     at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
System.err:     at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
System.err:     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
System.err:     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
System.err:     at android.os.Handler.handleCallback(Handler.java:751)
System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
System.err:     at android.os.Looper.loop(Looper.java:154)
System.err:     at android.app.ActivityThread.main(ActivityThread.java:6816)
System.err:     at java.lang.reflect.Method.invoke(Native Method)
System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1565)
System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1453)

我在BottoDialogFragment中编写了以下代码:

//      Log.i(TAG, "dragging()");
            Field mBehaviorField = bottomSheetDialog.getClass().getDeclaredField("mBehavior");
            mBehaviorField.setAccessible(true);
            final BottomSheetBehavior behavior =
                    (BottomSheetBehavior) mBehaviorField.get(bottomSheetDialog);
            behavior.setHideable(false);

并在下面的BottomDialogFragment移动事件中使用:

/*Back press event fire*/
        bottomSheetDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(android.content.DialogInterface dialog,
                                 int keyCode, android.view.KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
                    // To dismiss the fragment when the back-button is pressed.
                    Log.i(TAG, "onKey:KEYCODE_BACK ");
                    if (isRecordAudioAdd) {
                        if (mFilename != null && mFilename.length() > 0 && RecTime != 0) {
                            openDialog(mFilename, RecTime);
                        } else {
                            dismissAllowingStateLoss();
                        }
                        return true;
                    } else {
                        dismissAllowingStateLoss();
                    }
                    return true;
                }
                /*// Otherwise, do nothing else
                else return false;*/
                return false;
            }
        });

1 个答案:

答案 0 :(得分:1)

我已找到此警告消息的解决方案:

 /* ************** dragging off******************* */
    try {
        //      Log.i(TAG, "dragging()");
        Field mBehaviorField = bottomSheetDialog.getClass().getDeclaredField("behavior");
        mBehaviorField.setAccessible(true);
        final BottomSheetBehavior behavior =
                (BottomSheetBehavior) mBehaviorField.get(bottomSheetDialog);
        behavior.setHideable(false);

    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

将行为从bottomSheetDialog.getClass().getDeclaredField("mBehavior");更新为bottomSheetDialog.getClass().getDeclaredField("behavior");