1

次のコードを使用して、ユーザーが仮想キーボードでEnterキーを押した後に仮想キーボードを非表示にします。

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

1

エラーは次のとおりです。java.lang.ClassCastException: com.olacabs.customer.model.TextViewPlus cannot be cast to android.widget.EditText

へのキャストを 1 つだけ実行していますEditText:

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

R.id.nameTextはおそらく であり、com.olacabs.customer.model.TextViewPlusではありませんEditText。ということでキャスト変更。

于 2012-10-23T07:22:19.190 に答える