预览自定义TextInputLayout时出现问题

时间:2018-11-25 13:52:06

标签: android android-textinputlayout

我创建了一个自定义视图,其中封装了TextInputLayoutTextInputEditText。 该视图似乎在模拟器和硬件设备上均已正确渲染。 但是,当我在Android Studio中打开预览面板时,自定义视图无法正确呈现。

以下是我收到的任何提示的错误,敬请谅解。

  • Couldn't resolve resource @string/path_password_strike_through
  • <transition> tag requires a 'drawable' attribute or child tag defining a drawable
  • android.view.InflateException:  Can't morph from @string/path_password_eye_mask_strike_through to @string/path_password_eye_mask_visible
        at android.animation.AnimatorInflater.getPVH(AnimatorInflater.java:308)
        at android.animation.AnimatorInflater.parseAnimatorFromTypeArray(AnimatorInflater.java:422)
        at android.animation.AnimatorInflater.loadAnimator(AnimatorInflater.java:1053)
        at android.animation.AnimatorInflater.loadObjectAnimator(AnimatorInflater.java:1011)
        at android.animation.AnimatorInflater.createAnimatorFromXml(AnimatorInflater.java:667)
        at android.animation.AnimatorInflater.createAnimatorFromXml(AnimatorInflater.java:642)
        at android.animation.AnimatorInflater.loadAnimator(AnimatorInflater.java:126)
        at android.graphics.drawable.AnimatedVectorDrawable.inflate(AnimatedVectorDrawable.java:565)
        at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:142)
        at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1332)
        at android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1291)
        at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1266)
        at com.android.layoutlib.bridge.impl.ResourceHelper.getDrawable(ResourceHelper.java:330)
        at android.content.res.BridgeTypedArray.getDrawable(BridgeTypedArray.java:684)
        at android.graphics.drawable.AnimatedStateListDrawable.parseTransition(AnimatedStateListDrawable.java:473)
        at android.graphics.drawable.AnimatedStateListDrawable.inflateChildElements(AnimatedStateListDrawable.java:455)
        at android.graphics.drawable.AnimatedStateListDrawable.inflate(AnimatedStateListDrawable.java:385)
        at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:142)
        at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1332)
        at android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1291)
        at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1266)
        at com.android.layoutlib.bridge.impl.ResourceHelper.getDrawable(ResourceHelper.java:330)
        at android.content.res.Resources_Delegate.getDrawable(Resources_Delegate.java:186)
        at android.content.res.Resources.getDrawable(Resources.java:827)
        at android.content.Context.getDrawable(Context.java:626)
        at androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:463)
        at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:203)
        at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191)
        at androidx.appcompat.content.res.AppCompatResources.getDrawable(AppCompatResources.java:102)
        at androidx.appcompat.widget.TintTypedArray.getDrawable(TintTypedArray.java:76)
        at com.google.android.material.textfield.TextInputLayout.(TextInputLayout.java:344)
        at com.google.android.material.textfield.TextInputLayout.(TextInputLayout.java:247)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:401)
        at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:184)
        at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:142)
        at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:229)
        at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:421)
        at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:432)
        at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:336)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
        at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:327)
        at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:386)
        at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:193)
        at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:450)
        at com.android.tools.idea.rendering.RenderTask.lambda$inflate$3(RenderTask.java:590)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
    

已编辑(添加了代码示例)

自定义视图:



    public class MaterialTextInputView extends TextInputLayout {

      private TextInputEditText editText;

      public MaterialTextInputView(Context context) {
        super(context);
        init(context);
      }

      public MaterialTextInputView(Context context, AttributeSet) {
        super(context, attrs);

        init(context);
      }

      public MaterialTextInputView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
      }

      private void init(Context context) {
        TextInputEditText editText = new TextInputEditText(getContext());
        editText.setId(ViewCompat.generateViewId());

        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

        this.editText = editText;

        addView(this.editText, params);
        setErrorEnabled(true);
      }
    }

xml中的用法示例:

<com.marian.caloriecounter.ui.common.MaterialTextInputView
        android:id="@+id/foo"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="bar"
        style="@style/TextInputLayout"/>

1 个答案:

答案 0 :(得分:0)

确保您声明的变量未像String文件中的string/path_password_strike_through一样被解析,并且还声明了drawable文件夹中的drawable

如果没有您的代码,这将是非常模糊的...如果您分享它很容易获得帮助