Нет поля mCollapsingTextHelper в классе - PullRequest
1 голос
/ 06 апреля 2019

Здравствуйте, я хочу установить шрифт TextInputLayout, используя следующий код:

public class CustomFont {
    public static void setTypefaceToInputLayout(TextInputLayout inputLayout, String typeFace, Context context){

        final Typeface tf = Typeface.createFromAsset(context.getAssets(), typeFace);

        inputLayout.getEditText().setTypeface(tf);
        try {
            // Retrieve the CollapsingTextHelper Field
            final Field collapsingTextHelperField = inputLayout.getClass().getDeclaredField("mCollapsingTextHelper");
            collapsingTextHelperField.setAccessible(true);

            // Retrieve an instance of CollapsingTextHelper and its TextPaint
            final Object collapsingTextHelper = collapsingTextHelperField.get(inputLayout);
            final Field tpf = collapsingTextHelper.getClass().getDeclaredField("mTextPaint");
            tpf.setAccessible(true);

            // Apply your Typeface to the CollapsingTextHelper TextPaint
            ((TextPaint) tpf.get(collapsingTextHelper)).setTypeface(tf);
        } catch (Exception ignored) {
            // Nothing to do
            Log.e("errorXYZ","error:::"+ignored.toString());
        }
    }
}

Метод написан на Java, и я вызываю его из Kotlin как:

  CustomFont.setTypefaceToInputLayout(externalView.passwordTIL,"fonts/af/dragon.ttf",context!!)

Я пробовал с последних 24 часов, и по какой-то причине мне нужно любой ценой установить шрифт TextInputLayout (на самом деле шрифт с плавающей подсказкой)!

Я все время получаю следующее исключение:

 java.lang.NoSuchFieldException: No field mCollapsingTextHelper in class Landroid/support/design/widget/TextInputLayout; (declaration of 'android.support.design.widget.TextInputLayout' appears in /data/app/com.ar.ere-2/base.apk:classes2.dex)

Я понятия не имею об этом исключении, но для информации я включил Mutlidex в моем файле Gradle.Так я должен исключить некоторые файлы из Multidex lib?но как?

Понятия не имею!

Может кто-нибудь, пожалуйста, просмотрите ситуацию, пожалуйста!

1 Ответ

0 голосов
/ 06 апреля 2019

Вы используете отражение для доступа к полю, которого там нет.

TextInputLayout имеет метод setTypeface , который можно использовать для изменения как плавающей подсказки, так и любогосообщения об ошибках.

...