gpt4 book ai didi

android - 无法使用 Android Fragment 实现自定义键盘 - 空指针异常

转载 作者:行者123 更新时间:2023-11-30 02:54:06 24 4
gpt4 key购买 nike

我正在尝试使用以下示例实现自定义键盘:

http://www.fampennings.nl/maarten/android/09keyboard/index.htm

但是,我并没有像文章中描述的那样将它实现为一个 Activity - 我试图在一个 fragment 中实现它,但我似乎无法在不抛出 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 );

自定义键盘.java

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);
}

登录对话框.java

 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);
}

最佳答案

我刚刚遇到了同样的问题,我想我明白了。尝试像这样向 CustomKeyboard 类添加一个 View 变量:

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 已经想出了一些办法)但这是我第一次做出任何贡献,我有点兴奋。

关于android - 无法使用 Android Fragment 实现自定义键盘 - 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23611086/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com