当用户按下输入按钮时隐藏键盘

时间:2012-10-31 06:34:57

标签: android actionlistener

当用户按下enter键时,我使用以下代码隐藏键盘:

    friendSearch.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                EditText friendSearch=(EditText) findViewById(R.id.friendsearch);
                InputMethodManager imm = (InputMethodManager)getSystemService(
                          Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(friendSearch.getWindowToken(), 0);
                return true;
            }
            return false;
        }
    });

但是,当我按下键盘上的输入/下一个键(取决于设备)时,应用程序崩溃。我的错误日志cat如下:

10-31 12:27:00.092: E/ZygoteProcess(183): readInt++
10-31 12:27:00.109: E/Zygote(86): writeInt++17294
10-31 12:27:00.110: E/ZygoteProcess(183): readInt--: 17294
10-31 12:27:00.111: E/Zygote(86): writeInt--17294
10-31 12:27:07.700: E/AndroidRuntime(17280): FATAL EXCEPTION: main
10-31 12:27:07.700: E/AndroidRuntime(17280): java.lang.NullPointerException
10-31 12:27:07.700: E/AndroidRuntime(17280):    at com.olacabs.customer.activity.InviteFriendsFromContactsActivity$2.onEditorAction(InviteFriendsFromContactsActivity.java:134)
10-31 12:27:07.700: E/AndroidRuntime(17280):    at android.widget.TextView.onEditorAction(TextView.java:3641)
10-31 12:27:07.700: E/AndroidRuntime(17280):    at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:114)
10-31 12:27:07.700: E/AndroidRuntime(17280):    at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:322)
10-31 12:27:07.700: E/AndroidRuntime(17280):    at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:106)
10-31 12:27:07.700: E/AndroidRuntime(17280):    at android.os.Handler.dispatchMessage(Handler.java:130)
10-31 12:27:07.700: E/AndroidRuntime(17280):    at android.os.Looper.loop(SourceFile:351)
10-31 12:27:07.700: E/AndroidRuntime(17280):    at android.app.ActivityThread.main(ActivityThread.java:3814)
10-31 12:27:07.700: E/AndroidRuntime(17280):    at java.lang.reflect.Method.invokeNative(Native Method)
10-31 12:27:07.700: E/AndroidRuntime(17280):    at java.lang.reflect.Method.invoke(Method.java:538)
10-31 12:27:07.700: E/AndroidRuntime(17280):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
10-31 12:27:07.700: E/AndroidRuntime(17280):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:659)
10-31 12:27:07.700: E/AndroidRuntime(17280):    at dalvik.system.NativeStart.main(Native Method)
10-31 12:27:10.607: E/IPCThreadState(183): IPCThread::waitForResponse checking error 3 : -32
10-31 12:27:10.607: E/JavaBinder(183): BpBinder::transact() returns exception, err is -32
10-31 12:27:10.607: E/IPCThreadState(183): IPCThread::waitForResponse checking error 3 : -32
10-31 12:27:10.607: E/JavaBinder(183): BpBinder::transact() returns exception, err is -32
10-31 12:27:10.612: E/InputDispatcher(183): channel '405b46b8 com.olacabs.customer/com.olacabs.customer.activity.MyAccountActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
10-31 12:27:10.612: E/InputDispatcher(183): channel '405b46b8 com.olacabs.customer/com.olacabs.customer.activity.MyAccountActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
10-31 12:27:10.663: E/Zygote(86): writeInt++17303
10-31 12:27:10.665: E/ZygoteProcess(183): readInt++
10-31 12:27:10.666: E/Zygote(86): writeInt--17303
10-31 12:27:10.667: E/ZygoteProcess(183): readInt--: 17303
10-31 12:27:11.121: E/JavaBinder(183): BpBinder::transact() returns exception, err is -32
10-31 12:27:11.121: E/JavaBinder(183): BpBinder::transact() returns exception, err is -32
10-31 12:27:11.125: E/IPCThreadState(370): IPCThread::waitForResponse checking error 3 : -32
10-31 12:27:11.125: E/JavaBinder(370): BpBinder::transact() returns exception, err is -32
10-31 12:27:11.927: E/Crittercism(17303): Initialize the Crittercism library before using its methods.

我做错了什么,如何更改?

1 个答案:

答案 0 :(得分:6)

friendSearch.setOnEditorActionListener(new OnEditorActionListener()设置Listener之前初始化editText。

EditText friendSearch=(EditText) findViewById(R.id.friendsearch);
friendSearch.setOnEditorActionListener(new OnEditorActionListener() {

@Override
public boolean onEditorAction(TextView v, int keyCode,KeyEvent event) 
{
     if ((event.getAction() == KeyEvent.ACTION_DOWN  ) &&  (keyCode == KeyEvent.KEYCODE_ENTER)   )
     {               
       // hide virtual keyboard
       InputMethodManager imm = (InputMethodManager)getBaseContext().getSystemService(Context.INPUT_METHOD_SERVICE);
       imm.hideSoftInputFromWindow(friendSearch.getWindowToken(), 0);
       return true;
    }
    return false;
}});