public class MainActivity extends Activity { private SafeEditView pwdSEV; private Button loginBT; private boolean run = true; private int id = 0; private PopupWindow popupWindow; private KeyboardView mInputView; private Keyboard mQwertyKeyboard; View view; boolean is = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //pwdSEV = (SafeEditView)findViewById(R.id.passwd); loginBT = (Button)findViewById(R.id.loginbt); loginBT.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(!is){ popupWindow.setBackgroundDrawable(new BitmapDrawable()); popupWindow.setFocusable(false); popupWindow.setTouchable(true); popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0); //popupWindow.showAsDropDown(view); popupWindow.setOutsideTouchable(true); }else{ popupWindow.dismiss(); } is = !is; } }); mQwertyKeyboard = new Keyboard(getApplicationContext(), R.xml.qwerty); view = LayoutInflater.from(MainActivity.this).inflate( R.layout.safe_keyboard, null); mInputView = (KeyboardView) view.findViewById(R.id.keyboard_view); mInputView.setKeyboard(mQwertyKeyboard); popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, false); //设置可以获取焦点,否则弹出菜单中的EditText是无法获取输入的 popupWindow.setFocusable(true); //这句是为了防止弹出菜单获取焦点之后,点击activity的其他组件没有响应 popupWindow.setBackgroundDrawable(new BitmapDrawable()); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); } } 代码如上,自定义键盘可以弹出来,但是一点击就会出现下面的报错,哪位帮忙解答一下。 换个布局就不会出错。 04-07 14:40:07.725: E/AndroidRuntime(19100): java.lang.NullPointerException 04-07 14:40:07.725: E/AndroidRuntime(19100): at android.inputmethodservice.KeyboardView.onModifiedTouchEvent(KeyboardView.java:1211) 04-07 14:40:07.725: E/AndroidRuntime(19100): at android.inputmethodservice.KeyboardView.onTouchEvent(KeyboardView.java:1151) 04-07 14:40:07.725: E/AndroidRuntime(19100): at android.view.View.dispatchTouchEvent(View.java:3939) 04-07 14:40:07.725: E/AndroidRuntime(19100): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:902) 04-07 14:40:07.725: E/AndroidRuntime(19100): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:902) 04-07 14:40:07.725: E/AndroidRuntime(19100): at android.widget.PopupWindow$PopupViewContainer.dispatchTouchEvent(PopupWindow.java:1521) 04-07 14:40:07.725: E/AndroidRuntime(19100): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2364) 04-07 14:40:07.725: E/AndroidRuntime(19100): at android.view.ViewRoot.handleMessage(ViewRoot.java:2022) 04-07 14:40:07.725: E/AndroidRuntime(19100): at android.os.Handler.dispatchMessage(Handler.java:130) 04-07 14:40:07.725: E/AndroidRuntime(19100): at android.os.Looper.loop(Looper.java:384) 04-07 14:40:07.725: E/AndroidRuntime(19100): at android.app.ActivityThread.main(ActivityThread.java:3971) 04-07 14:40:07.725: E/AndroidRuntime(19100): at java.lang.reflect.Method.invokeNative(Native Method) 04-07 14:40:07.725: E/AndroidRuntime(19100): at java.lang.reflect.Method.invoke(Method.java:538) 04-07 14:40:07.725: E/AndroidRuntime(19100): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978) 04-07 14:40:07.725: E/AndroidRuntime(19100): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:732) 04-07 14:40:07.725: E/AndroidRuntime(19100): at dalvik.system.NativeStart.main(Native Method) |
|
10分 |
ndroid.inputmethodservice.KeyboardView.onModifiedTouchEvent(KeyboardView.java:1211)
空异常啊,你看看这行为什么是空的对象。 KeyboardView mInputView = (KeyboardView) view.findViewById(R.id.keyboard_view); |
10分 |
貌似id没获取到?
|
就是看不到呀,是不是 keyboardview 本身就有 popwindow,所以应该要 重写 ontouch 事件? |
|
10分 |
你这个还是获取焦点的问题,popupWindow弹出来后,他获得了焦点,你在EditText中加个监听:setOnFocusChangeListener;
注意:你可以判断PopupWindow.isshow(),然后,添加监听setOnFocusChangeListener,dismiss就失去焦点,如果失去焦点的时候有异常,那么尝试在失去焦点的时候,让其他控件获得焦点。 http://dwtedx.com/itshare_313.html 按这里的方法应该可以解决。 |
10分 |
NullPointerException 这个异常解决思路,先找出空指针的变量,再看是因为什么成为空指针就可以了。
|
不是 焦点的 原因。 |