1

我正在使用以下代码使虚拟键盘在用户按下回车后消失:

nameText.setOnEditorActionListener(new OnEditorActionListener() {

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

但是,当我按 Enter 键时,应用程序崩溃了。代码有什么问题

错误日志cat如下:

10-23 12:37:15.507: E/AndroidRuntime(2969): FATAL EXCEPTION: main
10-23 12:37:15.507: E/AndroidRuntime(2969): java.lang.ClassCastException: com.olacabs.customer.model.TextViewPlus cannot be cast to android.widget.EditText
10-23 12:37:15.507: E/AndroidRuntime(2969):     at com.olacabs.customer.activity.MyProfileActivity$3.onEditorAction(MyProfileActivity.java:173)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at android.widget.TextView.doKeyDown(TextView.java:5611)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at android.widget.TextView.onKeyDown(TextView.java:5503)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at android.view.KeyEvent.dispatch(KeyEvent.java:2668)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at android.view.View.dispatchKeyEvent(View.java:5527)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1266)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1266)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1266)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1266)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1266)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1266)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1266)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1928)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1388)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at android.app.Activity.dispatchKeyEvent(Activity.java:2388)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1855)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3604)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2848)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at android.os.Looper.loop(Looper.java:154)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at android.app.ActivityThread.main(ActivityThread.java:4974)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at java.lang.reflect.Method.invokeNative(Native Method)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at java.lang.reflect.Method.invoke(Method.java:511)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-23 12:37:15.507: E/AndroidRuntime(2969):     at dalvik.system.NativeStart.main(Native Method)
10-23 12:37:15.527: E/EmbeddedLogger(1613): App crashed! Process: com.olacabs.customer
10-23 12:37:15.527: E/EmbeddedLogger(1613): App crashed! Package: com.olacabs.customer v2001 (2.0.0.1)
10-23 12:37:15.527: E/EmbeddedLogger(1613): Application Label: Ola Cabs
10-23 12:37:17.399: E/InputDispatcher(1613): channel '40e86a78 com.olacabs.customer/com.olacabs.customer.activity.PickUpActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
10-23 12:37:17.419: E/InputDispatcher(1613): channel '416bd7f8 com.olacabs.customer/com.olacabs.customer.activity.MyProfileActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
10-23 12:37:17.429: E/InputDispatcher(1613): channel '418c7898 PopupWindow:4105d7e8 (server)' ~ Channel is unrecoverably broken and will be disposed!
10-23 12:37:17.429: E/InputDispatcher(1613): channel '41c40030 com.olacabs.customer/com.olacabs.customer.activity.MyAccountActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
10-23 12:37:17.579: E/ActivityManager(1613): fail to set top app changed! (2)
10-23 12:37:17.579: E/ActivityManager(1613): java.lang.NullPointerException
10-23 12:37:17.579: E/ActivityManager(1613):    at com.android.server.am.ActivityManagerService.handleTopAppChanged(ActivityManagerService.java:15869)
10-23 12:37:17.579: E/ActivityManager(1613):    at com.android.server.am.ActivityManagerService.updateOomAdjLocked(ActivityManagerService.java:15319)
10-23 12:37:17.579: E/ActivityManager(1613):    at com.android.server.am.ActivityManagerService.updateLruProcessInternalLocked(ActivityManagerService.java:1942)
10-23 12:37:17.579: E/ActivityManager(1613):    at com.android.server.am.ActivityManagerService.updateLruProcessLocked(ActivityManagerService.java:1949)
10-23 12:37:17.579: E/ActivityManager(1613):    at com.android.server.am.ActivityStack.realStartActivityLocked(ActivityStack.java:626)
10-23 12:37:17.579: E/ActivityManager(1613):    at com.android.server.am.ActivityManagerService.attachApplicationLocked(ActivityManagerService.java:4285)
10-23 12:37:17.579: E/ActivityManager(1613):    at com.android.server.am.ActivityManagerService.attachApplication(ActivityManagerService.java:4373)
10-23 12:37:17.579: E/ActivityManager(1613):    at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:347)
10-23 12:37:17.579: E/ActivityManager(1613):    at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:1714)
10-23 12:37:17.579: E/ActivityManager(1613):    at android.os.Binder.execTransact(Binder.java:338)
10-23 12:37:17.579: E/ActivityManager(1613):    at dalvik.system.NativeStart.run(Native Method)
10-23 12:37:18.169: E/MapActivity(3867): Couldn't get connection factory client
4

2 回答 2

3

所以你得到 java.lang.ClassCastException: com.olacabs.customer.model.TextViewPlus cannot be cast to android.widget.EditText.

这意味着这EditText nameText=(EditText) findViewById(R.id.nameText);是错误的。

findViewById(R.id.nameText)返回的不是一个,EditText而是一个TextViewPlus实例。

于 2012-10-23T07:18:27.617 回答
1

你的错误是:java.lang.ClassCastException: com.olacabs.customer.model.TextViewPlus cannot be cast to android.widget.EditText

你只做一次演员EditText

EditText nameText=(EditText) findViewById(R.id.nameText);

R.id.nameText可能是 acom.olacabs.customer.model.TextViewPlus而不是EditText. 所以改变你的演员阵容。

于 2012-10-23T07:22:19.190 回答