恼人的键盘错误,致命异常:IllegalArgumentException参数必须是此视图的后代

时间:2016-01-27 02:56:50

标签: android keyboard android-input-method

我在Activity中包含了2个片段,当我打开第二个Fragment时,键盘会立即弹出并要求您输入内容。

有两种方法可以关闭键盘。

  1. 按下(我有一个自定义的EditText,当您按下时也会关闭片段)
  2. 在打开的第二个片段上向上或向下滑动,它会关闭键盘和片段。
  3. 发生的烦人的错误是,当我关闭第二个片段时,它会关闭片段和键盘,但是当你回到第一个片段时,键盘会再次弹回,如果用户在屏幕上的任何位置滑动,就会发生致命异常

    以下是关于如何关闭第二片段上的键盘的代码:

    当用户在第二个片段内的RelativeLayout上滑动时,这是被调用的代码块:

    relativeLayout.setOnTouchListener(new View.OnTouchListener() {
    
            float startY = 0, endY = 0;
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
    
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        startY = event.getY();
                        break;
    
                    case MotionEvent.ACTION_UP:
                        endY = event.getY();
    
                        if(Math.abs(startY - endY) > 200) {
                            onHideSoftKeyboard(editText);
                            getFragmentManager().popBackStack();
                        }
    
                        break;
                }
    
                return false;
            }
        });
    
     public void onHideSoftKeyboard(final EditText editText) {
        if (mContextWeakReference.get() != null) {
            final InputMethodManager imm = (InputMethodManager) mContextWeakReference.get().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        }
     }
    

    用户发生异常:

    Fatal Exception: java.lang.IllegalArgumentException: parameter must be a descendant of this view
       at android.view.ViewGroup.offsetRectBetweenParentAndChild(ViewGroup.java:5191)
       at android.view.ViewGroup.offsetDescendantRectToMyCoords(ViewGroup.java:5128)
       at android.view.ViewRootImpl.scrollToRectOrFocus(ViewRootImpl.java:2978)
       at android.view.ViewRootImpl.draw(ViewRootImpl.java:2647)
       at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2577)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2169)
       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1180)
       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6563)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
       at android.view.Choreographer.doCallbacks(Choreographer.java:590)
       at android.view.Choreographer.doFrame(Choreographer.java:560)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:145)
       at android.app.ActivityThread.main(ActivityThread.java:5835)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
    

    我该如何解决这个问题?

0 个答案:

没有答案