2012-10-23 209 views
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; 
       } 
      }); 

然而,當我按回車鍵,應用程序崩潰。什麼是錯的代碼

錯誤日誌貓如下:

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 
+2

在此粘貼來自logcat的堆棧跟蹤。 –

+0

我編輯了我的問題 – Ankush

回答

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實例。

+0

謝謝!我改變了'EditText nameText =(EditText)findViewById(R.id.nameText);'爲'TextView nameText =(TextView)findViewById(R.id.nameText);'現在它可以工作 – Ankush

+0

@ user1696863:酷! :)順便說一句,UI組件被放入一個暗示模型放置位置的模塊(就模型 - 視圖 - 控制器設計模式而言),似乎有點奇怪。 –

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可能是一個com.olacabs.customer.model.TextViewPlus而不是EditText。所以改變你的演員。