3

In my actionbar menu I have a searchview like this:

<item android:id="@+id/action_search"
    android:title="@string/action_search"
    android:icon="@drawable/ic_search_black_36dp"
    app:showAsAction="ifRoom|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView" />

the search icon does appear on the actionbar and when I click on it a text input field appears with a back button besides it and keyboard opens.

enter image description here enter image description here

If i close the keyboard and press the back button the text field disappears but if I click on the back button when keyboard is open I get this error:

04-07 15:57:08.411  14736-14736/cheetar.xorjin E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalArgumentException: parameter must be a descendant of this view
        at android.view.ViewGroup.offsetRectBetweenParentAndChild(ViewGroup.java:4257)
        at android.view.ViewGroup.offsetDescendantRectToMyCoords(ViewGroup.java:4194)
        at android.view.ViewRootImpl.scrollToRectOrFocus(ViewRootImpl.java:2122)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:1842)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1634)
        at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4448)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        at dalvik.system.NativeStart.main(Native Method)

I used this answer to close the keyboard whenever something other than a textview is touched and it works fine except for when I click on that back button. How can I fix this?

If more information is needed please tell me what it is and I'll add it asap.

Thank you

Community
  • 1
  • 1
soroosh.strife
  • 1,181
  • 4
  • 19
  • 45
  • We will need more code to reproduce this error. And what you mean with this "If i close the keyboard and press the back button the text field disappears", how you close the keyboard? i have tested on other app and when TextField exist 1 back button click close keyboard, 2 back click hide TextField. Are you getting this exception on 2 click? – Sulfkain Apr 13 '15 at 08:50
  • I don't have any code in my activity related to this part yet (have not implemented search). Weirdly enough it happens randomly in different builds. And sometimes even if I don't change anything it happens or gets fixed. Right now I can't reproduce the error myself and am very confused. Also the error happened when I pressed the back button on the tool bar. If I pressed my device back button 2 times it worked fine because it would first close the keyboard. – soroosh.strife Apr 13 '15 at 16:14
  • Ahh ok, you mean, not the device back button but the tool bar "<-". Please, debug your code line by line, to see which view raise the exception (and which method). We can help you then more – Sulfkain Apr 14 '15 at 07:09
  • put your back button code – Jemshit Apr 16 '15 at 05:41

2 Answers2

2

The best way to hide the keyboard on Android is to crate a dummy view and request a focus on it when you want to dismiss the keyboard. Hiding keyboard will give you headaches like this one. When you want the keyboard to come out again, request a focus on edit text... that way you can be sure that the keyboard will be the right type (defined by android:inputType="...") Tough I was using this for a while

import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

public class KeyboardUtils {

    public static void hideKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    public static void showKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInputFromInputMethod(view.getWindowToken(), 0);
    }

}
Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59
0

Did you try to implement some solutions from SO to your exception? For example check this answer https://stackoverflow.com/a/12888761/3225458, and try to clear the current focus in your Activity inside onTouchListener of each view before hideSoftKeyboard.

Community
  • 1
  • 1
romtsn
  • 11,704
  • 2
  • 31
  • 49