Maihan Nijat Maihan Nijat - 3 years ago 295
Java Question

Why createKeyFromXml() doesn't create language switch key?

I am working on Android Custom Keyboard and it crashes when I launch. I found

method has a line which creates keys and those keys doesn't create language switch key which cause the app to crash.

The developer guide doesn't say much about it:


class LatinKeyboard extends Keyboard {
private Key mEnterKey;
private Key mSpaceKey;
private Key mModeChangeKey;
private Key mLanguageSwitchKey;
private Key mSavedModeChangeKey;
private Key mSavedLanguageSwitchKey;

LatinKeyboard(Context context, int xmlLayoutResId) {
super(context, xmlLayoutResId);
public LatinKeyboard(Context context, int layoutTemplateResId,
CharSequence characters, int columns, int horizontalPadding) {
super(context, layoutTemplateResId, characters, columns, horizontalPadding);
protected Key createKeyFromXml(Resources res, Row parent, int x, int y,
XmlResourceParser parser) {
Key key = new LatinKey(res, parent, x, y, parser);
Log.d("Keys", String.valueOf(key));
if ([0] == 10) {
mEnterKey = key;
} else if ([0] == ' ') {
mSpaceKey = key;
} else if ([0] == Keyboard.KEYCODE_MODE_CHANGE) {
mModeChangeKey = key;
mSavedModeChangeKey = new LatinKey(res, parent, x, y, parser);
} else if ([0] == LatinKeyboardView.KEYCODE_LANGUAGE_SWITCH) {
mLanguageSwitchKey = key;
mSavedLanguageSwitchKey = new LatinKey(res, parent, x, y, parser);
return key;

void setLanguageSwitchKeyVisibility(boolean visible) {
if (visible) {
mModeChangeKey.width = mSavedModeChangeKey.width;
mModeChangeKey.x = mSavedModeChangeKey.x;
mLanguageSwitchKey.width = mSavedLanguageSwitchKey.width;
mLanguageSwitchKey.icon = mSavedLanguageSwitchKey.icon;
mLanguageSwitchKey.iconPreview = mSavedLanguageSwitchKey.iconPreview;
} else {
mModeChangeKey.width = mSavedModeChangeKey.width + mSavedLanguageSwitchKey.width;
mLanguageSwitchKey.width = 0;
mLanguageSwitchKey.icon = null;
mLanguageSwitchKey.iconPreview = null;
void setImeOptions(Resources res, int options) {
if (mEnterKey == null) {
switch (options&(EditorInfo.IME_MASK_ACTION|EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
case EditorInfo.IME_ACTION_GO:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.label = res.getText(R.string.label_go_key);
case EditorInfo.IME_ACTION_NEXT:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.label = res.getText(R.string.label_next_key);
case EditorInfo.IME_ACTION_SEARCH:
mEnterKey.icon = res.getDrawable(R.drawable.ic_search_24dp);
mEnterKey.label = null;
case EditorInfo.IME_ACTION_SEND:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.label = res.getText(R.string.label_send_key);
mEnterKey.icon = res.getDrawable(R.drawable.ic_check_circle_24dp);
mEnterKey.label = null;
void setSpaceIcon(final Drawable icon) {
if (mSpaceKey != null) {
mSpaceKey.icon = icon;
private static class LatinKey extends Keyboard.Key {

LatinKey(Resources res, Keyboard.Row parent, int x, int y,
XmlResourceParser parser) {
super(res, parent, x, y, parser);

public boolean isInside(int x, int y) {
return super.isInside(x, codes[0] == KEYCODE_CANCEL ? y - 10 : y);

The error log is:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sunzala.afghankeyboard, PID: 8894
at android.inputmethodservice.InputMethodService.initialize(

I understand the app crashes because
is null and that's because of the method I mentioned. I checked the method and it runs but it doesn't create key which match switch language key code.

I uploaded the app on GitHub if someone is interested to have a look:

Answer Source

From the sample code mentioned in another question shows that NullPointerException will be thrown if "nextKeyboard" is null.

private void setLatinKeyboard(LatinKeyboard nextKeyboard) {
    final boolean shouldSupportLanguageSwitchKey =
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download