0

我想在Android库项目中使用TextInputLayout。但是,我在设计使用XML的布局时遇到了错误,并且在执行该项目时出现了'android.view.InflateException'。我的XML布局文件是错误在Android库项目中使用TextInputLayout

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/root" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <CustomViewPager 
     android:id="@+id/viewPager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:gravity="bottom"> 

     <android.support.design.widget.TextInputLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <EditText 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:imeOptions="actionDone" 
       android:inputType="textNoSuggestions" /> 
     </android.support.design.widget.TextInputLayout> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Skip"/> 
    </RelativeLayout> 
</RelativeLayout> 

在版面设计窗口还我得到以下错误

java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library. 
    at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:36) 
    at android.support.design.widget.TextInputLayout.<init>(TextInputLayout.java:184) 
    at android.support.design.widget.TextInputLayout.<init>(TextInputLayout.java:177) 
    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:465) 
    at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:172) 
    at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:105) 
    at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:186) 
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:334) 
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:345) 
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:245) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727) 
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:858) 
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:834) 
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821) 
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:861) 
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:834) 
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:518) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:324) 
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:429) 
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:389) 
    at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:548) 
    at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:533) 
    at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:966) 
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:533) 
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate$53(RenderTask.java:659) 
    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) 

项目minSDK是16,targetSDK是24和compileSDK是24与构建工具版本24.0.1。此外,我还在应用程序和库模块中添加了下面的gradle依赖关系。

compile 'com.android.support:appcompat-v7:24.2.1' 
compile 'com.android.support:design:24.2.1' 
compile 'com.android.support:support-v4:24.2.1' 
+0

发布包含此视图的xml文件 – mklimek

+0

@mklimek更新了XML布局文件 – anandprabhu

回答

0

您应该使用TextInputEditTextTextInputLayout

<android.support.design.widget.TextInputLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <android.support.design.widget.TextInputEditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

</android.support.design.widget.TextInputLayout> 

确保您的应用程序使用程序兼容性主题

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 

或其他延伸它像Theme.AppCompat.Light.DarkActionBar

最后,AndroidManifest.xml中:

<application android:theme="@style/AppTheme"> 
+0

实际上,我的布局文件位于不在应用程序内的库模块内。我尝试设置主应用程序的主题,但错误未在库模块内部解决。 – anandprabhu

+0

确保您的活动不会覆盖主题设置 – mklimek

1

最后将一个AppCompat主题直接添加到TextInputLayout使其正常工作。我只是遵循以下提到的职位提供的解决方案,

TextInputLayout AppCompat Theme

我更新了我的布局文件如下面,

<android.support.design.widget.TextInputLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/Theme.AppCompat"> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:focusable="true" 
      android:focusableInTouchMode="true" 
      android:imeOptions="actionDone" 
      android:inputType="textNoSuggestions" /> 
    </android.support.design.widget.TextInputLayout> 
0

您的活动必须延伸AppCompatActivity。然后只有它为我工作。