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)