Blankj/AndroidUtilCode · error · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

KeyboardUtils is a final utility class providing static methods for soft-input keyboard control (showSoftInput, hideSoftInput, etc.). Its private constructor throws UnsupportedOperationException to prevent instantiation, enforcing static-only access.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/KeyboardUtils.java:37

import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.lang.reflect.Field;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/08/02
 *     desc  : utils about keyboard
 * </pre>
 */
public final class KeyboardUtils {

    private static final int TAG_ON_GLOBAL_LAYOUT_LISTENER = -8;

    private KeyboardUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    /**
     * Show the soft input.
     */
    public static void showSoftInput() {
        InputMethodManager imm =
            (InputMethodManager) Utils.getApp().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm == null) {
            return;
        }
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

    /**
     * Show the soft input.
     */
    public static void showSoftInput(@Nullable Activity activity) {

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Call static methods directly: KeyboardUtils.showSoftInput(), KeyboardUtils.hideSoftInput(view). Never instantiate.
  2. Exclude utility classes from DI component scanning or serialization configuration.
  3. For testing, use mockito-inline or PowerMock to mock static methods without instantiation.
  4. If reflection is the cause, verify the target class is not a utility before calling newInstance().

Example fix

// before
KeyboardUtils kb = new KeyboardUtils();
kb.showSoftInput();

// after
KeyboardUtils.showSoftInput();
Defensive patterns

Strategy: type-guard

Validate before calling

// Only call static methods; verify before reflective instantiation
if (target != KeyboardUtils.class) {
    target.getDeclaredConstructor().newInstance();
}

Type guard

static boolean isUtilityClass(Class<?> clazz) {
    return Modifier.isFinal(clazz.getModifiers())
        && clazz.getName().endsWith("Utils");
}

Try / catch

try {
    KeyboardUtils.class.getDeclaredConstructor().setAccessible(true).newInstance();
} catch (UnsupportedOperationException e) {
    // Utility class — switch to static method calls
}

Prevention

When it happens

Trigger: Direct instantiation via reflection, or frameworks (DI containers, mocking libraries, serialization) that attempt to construct KeyboardUtils by calling its constructor through reflection.

Common situations: Mockito or PowerMock tries to create an instance for static-method mocking; a DI framework scans the utility package and attempts construction; a test runner instantiates all classes in a package.

Related errors


AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14). Data as JSON: /api/errors/7b96e50c7d03c42d. Report an issue: GitHub.