Maihan Nijat
Maihan Nijat

Reputation: 9334

Why soft keyboard app crashes because of java.lang.NullPointerException?

I have published Android soft-keyboard on Play Store and found 5 crashes in one day for the following devices:

enter image description here

The following is the crash report:

java.lang.NullPointerException: Attempt to read from field 'int android.inputmethodservice.Keyboard$Key.width' on a null object reference
    at com.sunzala.afghankeyboard.android.LatinKeyboard.setLanguageSwitchKeyVisibility(LatinKeyboard.java:93)
    at com.sunzala.afghankeyboard.android.SoftKeyboard.setLatinKeyboard(SoftKeyboard.java:210)
    at com.sunzala.afghankeyboard.android.SoftKeyboard.onStartInputView(SoftKeyboard.java:368)
    at android.inputmethodservice.InputMethodService.showWindowInner(InputMethodService.java:2187)
    at android.inputmethodservice.InputMethodService.showWindow(InputMethodService.java:2081)
    at android.inputmethodservice.InputMethodService$InputMethodImpl.showSoftInput(InputMethodService.java:651)
    at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:216)
    at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:37)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7237)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Here is the code for LatinKeyboard.setLanguageSwitchKeyVisibility(LatinKeyboard.java:93:

    public class LatinKeyboard extends Keyboard {

    private Key mEnterKey;
    private Key mSpaceKey;
    /**
     * Stores the current state of the mode change key. Its width will be dynamically updated to
     * match the region of {@link #mModeChangeKey} when {@link #mModeChangeKey} becomes invisible.
     */
    private Key mModeChangeKey;
    /**
     * Stores the current state of the language switch key (a.k.a. globe key). This should be
     * returns true. When this key becomes invisible, its width will be shrunk to zero.
     */
    private Key mLanguageSwitchKey;
    /**
     * Stores the size and other information of {@link #mModeChangeKey} when
     * {@link #mLanguageSwitchKey} is visible. This should be immutable and will be used only as a
     * reference size when the visibility of {@link #mLanguageSwitchKey} is changed.
     */
    private Key mSavedModeChangeKey;
    /**
     * Stores the size and other information of {@link #mLanguageSwitchKey} when it is visible.
     * This should be immutable and will be used only as a reference size when the visibility of
     * {@link #mLanguageSwitchKey} is changed.
     */
    private Key mSavedLanguageSwitchKey;

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

    @Override
    protected Key createKeyFromXml(Resources res, Row parent, int x, int y,
                                   XmlResourceParser parser) {
        Key key = new LatinKey(res, parent, x, y, parser);
        if (key.codes[0] == 10) {
            mEnterKey = key;
        } else if (key.codes[0] == ' ') {
            mSpaceKey = key;
        } else if (key.codes[0] == Keyboard.KEYCODE_MODE_CHANGE) {
            mModeChangeKey = key;
            mSavedModeChangeKey = new LatinKey(res, parent, x, y, parser);
        } else if (key.codes[0] == LatinKeyboardView.KEYCODE_LANGUAGE_SWITCH) {
            mLanguageSwitchKey = key;
            mSavedLanguageSwitchKey = new LatinKey(res, parent, x, y, parser);
        }
        return key;
    }

    /**
     * Dynamically change the visibility of the language switch key (a.k.a. globe key).
     *
     * @param visible True if the language switch key should be visible.
     */
    void setLanguageSwitchKeyVisibility(boolean visible) {
        if (visible) {
            // The language switch key should be visible. Restore the size of the mode change key
            // and language switch key using the saved layout.
            mModeChangeKey.width = mSavedModeChangeKey.width;
            mModeChangeKey.x = mSavedModeChangeKey.x;
            mLanguageSwitchKey.width = mSavedLanguageSwitchKey.width;
            mLanguageSwitchKey.icon = mSavedLanguageSwitchKey.icon;
            mLanguageSwitchKey.iconPreview = mSavedLanguageSwitchKey.iconPreview;
        } else {
            // The language switch key should be hidden. Change the width of the mode change key
            // to fill the space of the language key so that the user will not see any strange gap.
            mModeChangeKey.width = mSavedModeChangeKey.width + mSavedLanguageSwitchKey.width;
            mLanguageSwitchKey.width = 0;
            mLanguageSwitchKey.icon = null;
            mLanguageSwitchKey.iconPreview = null;
        }
    }

    /**
     * This looks at the ime options given by the current editor, to set the
     * appropriate label on the keyboard's enter key (if it has one).
     */
    void setImeOptions(Resources res, int options) {
        if (mEnterKey == null) {
            return;
        }

        switch (options & (EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
            case EditorInfo.IME_ACTION_GO:
                mEnterKey.label = null;
                mEnterKey.icon = res.getDrawable(R.drawable.ic_go_circle_filled_24dp);
                break;
            case EditorInfo.IME_ACTION_NEXT:
                mEnterKey.label = null;
                mEnterKey.icon = res.getDrawable(R.drawable.ic_next_circle_filled_24dp);
                break;
            case EditorInfo.IME_ACTION_SEARCH:
                mEnterKey.icon = res.getDrawable(R.drawable.ic_search_24dp);
                mEnterKey.label = null;
                break;
            case EditorInfo.IME_ACTION_SEND:
                mEnterKey.label = null;
                mEnterKey.icon = res.getDrawable(R.drawable.ic_send_24dp);
                break;
            default:
                mEnterKey.label = null;
                mEnterKey.icon = res.getDrawable(R.drawable.ic_check_circle_24dp);
                break;
        }
    }

    void setSpaceIcon(final Drawable icon) {
        if (mSpaceKey != null) {
            mSpaceKey.icon = icon;
        }
    }

    static class LatinKey extends Keyboard.Key {

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

        /**
         * Overriding this method so that we can reduce the target area for the key that
         * closes the keyboard.
         */
        @Override
        public boolean isInside(int x, int y) {
            return super.isInside(x, codes[0] == KEYCODE_CANCEL ? y - 10 : y);
        }
    }

}

Here is the code for SoftKeyboard.setLatinKeyboard(SoftKeyboard.java:210) nextKeyboard.setLanguageSwitchKeyVisibility(shouldSupportLanguageSwitchKey);

Edit: I believe the issue caused because of the following code:

private void setLatinKeyboard(LatinKeyboard nextKeyboard) {
    boolean shouldSupportLanguageSwitchKey = true;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
        shouldSupportLanguageSwitchKey = mInputMethodManager.shouldOfferSwitchingToNextInputMethod(getToken());
    }
    nextKeyboard.setLanguageSwitchKeyVisibility(shouldSupportLanguageSwitchKey);
    mInputView.setKeyboard(nextKeyboard);
}

I tried to run the soft-keyboard on emulator for Android version 5.0 and 6.0 but didn't get any error. What cause the error and how can I fix it?

Upvotes: 0

Views: 1094

Answers (1)

iheanyi
iheanyi

Reputation: 3113

Looks like you initialize mModeChangedKey and mSavedModeChangeKey based on key.codes in createKeyFromXml() however setLanguageSwitchKeyVisibility() will use one of those keys based on a completely different parameter - visibility.

So, what's happening is that one key is being created in createKeyFromXml() but the other key (still null referenced) is being used in setLanguageSwitchKeyVisibility().

Upvotes: 3

Related Questions