我正在尝试使用以下示例实现自定义键盘:
http://www.fampennings.nl/maarten/android/09keyboard/index.htm
然而,不是像文章中描述的那样实现它的活动 - 我试图在一个片段中实现它,我似乎无法在不抛弃NPE的情况下使其工作。
我不确定在这种情况下我做错了什么 - 但是非常感谢任何建议/输入。
05-12 09:48:10.300: E/AndroidRuntime(3664): FATAL EXCEPTION: main
05-12 09:48:10.300: E/AndroidRuntime(3664): java.lang.NullPointerException
05-12 09:48:10.300: E/AndroidRuntime(3664): at com.example.project.CustomKeyboard.<init>(CustomKeyboard.java:111)
05-12 09:48:10.300: E/AndroidRuntime(3664): at com.example.project.LoginDialog.onCreateDialog(LoginDialog.java:64)
05-12 09:48:10.300: E/AndroidRuntime(3664): at android.app.DialogFragment.getLayoutInflater(DialogFragment.java:398)
05-12 09:48:10.300: E/AndroidRuntime(3664): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
05-12 09:48:10.300: E/AndroidRuntime(3664): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
05-12 09:48:10.300: E/AndroidRuntime(3664): at android.app.BackStackRecord.run(BackStackRecord.java:682)
05-12 09:48:10.300: E/AndroidRuntime(3664): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
05-12 09:48:10.300: E/AndroidRuntime(3664): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
05-12 09:48:10.300: E/AndroidRuntime(3664): at android.os.Handler.handleCallback(Handler.java:725)
05-12 09:48:10.300: E/AndroidRuntime(3664): at android.os.Handler.dispatchMessage(Handler.java:92)
05-12 09:48:10.300: E/AndroidRuntime(3664): at android.os.Looper.loop(Looper.java:137)
05-12 09:48:10.300: E/AndroidRuntime(3664): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-12 09:48:10.300: E/AndroidRuntime(3664): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 09:48:10.300: E/AndroidRuntime(3664): at java.lang.reflect.Method.invoke(Method.java:511)
05-12 09:48:10.300: E/AndroidRuntime(3664): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-12 09:48:10.300: E/AndroidRuntime(3664): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-12 09:48:10.300: E/AndroidRuntime(3664): at dalvik.system.NativeStart.main(Native Method)
CustomKeyboard.java:111
是
mKeyboardView.setKeyboard(new Keyboard(mHostActivity, layoutid));
LoginDialog.java:64
是
mCustomKeyboard= new CustomKeyboard(this, R.id.keyboardview, R.xml.hexkbd );
public CustomKeyboard(LoginDialog loginDialog, int viewid, int layoutid) {
mHostActivity= loginDialog.getActivity();
mKeyboardView= (KeyboardView)mHostActivity.findViewById(viewid);
mKeyboardView.setKeyboard(new Keyboard(mHostActivity, layoutid));
mKeyboardView.setPreviewEnabled(false); // NOTE Do not show the preview balloons
mKeyboardView.setOnKeyboardActionListener(mOnKeyboardActionListener);
// Hide the standard keyboard initially
mHostActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
public class LoginDialog extends DialogFragment implements ActionCompletedListener{
private SingletonVariables variables;
private View view;
private TextView error;
private String whichActivity = "";
CustomKeyboard mCustomKeyboard;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(),R.style.HoloDarkDialog));
LayoutInflater inflater = getActivity().getLayoutInflater();
variables = SingletonVariables.getInstance();
view = inflater.inflate(R.layout.login, null);
EditText userEditText = (EditText) view.findViewById(R.id.loginUserIdEditText);
mCustomKeyboard= new CustomKeyboard(this, R.id.keyboardview, R.xml.hexkbd );
mCustomKeyboard.registerEditText(R.id.loginUserIdEditText);
mCustomKeyboard.registerEditText(R.id.loginPasswordEditText);
if(variables.login.sessionPassword != null) {
userEditText.setText(variables.login.sessionUser);
}
答案 0 :(得分:1)
我遇到了同样的问题,我想我明白了。尝试将View变量添加到CustomKeyboard类,如下所示:
private View mRootView;
然后修改构造函数:
public CustomKeyboard(Activity host, View rootView, int viewid, int layoutid) {
mHostActivity= host;
mRootView = rootView;
mKeyboardView= (KeyboardView)mRootView.findViewById(viewid);
mKeyboardView.setKeyboard(new Keyboard(mHostActivity, layoutid));
mKeyboardView.setPreviewEnabled(false); // NOTE Do not show the preview balloons
mKeyboardView.setOnKeyboardActionListener(mOnKeyboardActionListener);
mHostActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
修改registerEditText()以便:
EditText edittext= (EditText)mHostActivity.findViewById(resid);
变为:
EditText edittext= (EditText)mRootView.findViewById(resid);
然后更改DialogFragment onCreateDialog()中的调用,如下所示:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(),R.style.HoloDarkDialog));
LayoutInflater inflater = getActivity().getLayoutInflater();
variables = SingletonVariables.getInstance();
view = inflater.inflate(R.layout.login, null);
EditText userEditText = (EditText) view.findViewById(R.id.loginUserIdEditText);
mCustomKeyboard= new CustomKeyboard(getActivity() , view , R.id.keyboardview, R.xml.hexkbd );
mCustomKeyboard.registerEditText(R.id.loginUserIdEditText);
mCustomKeyboard.registerEditText(R.id.loginPasswordEditText);
if(variables.login.sessionPassword != null) {
userEditText.setText(variables.login.sessionUser);
}
我希望这有助于某人(我想OP已经找到了一些东西),但这是我第一次有所贡献,我有点兴奋。
答案 1 :(得分:0)
您的问题是,当您实例化LoginDialog
时,Activity
未附加到任何CustomKeyboard
,因此:
loginDialog.getActivity() is null
您可以使用回调onAttach
来实例化CustomKeyboard,因为此时对话框已附加到活动
试试这个:
public void onAttach(Activity hostActivity) {
super.onAttach(hostActivity);
mCustromKeyboard = new CustomKeyboard(activity, R.id.keyboardview, R.xml.hexkbd ));
mCustromKeyboard.registerEditText(R.id.loginUserIdEditText);
mCustromKeyboard.registerEditText(R.id.loginPasswordEditText);
}