android.support.design.widget.TextInputLayout gives InflateException

拜拜、爱过 提交于 2019-12-08 06:19:55

问题


I am trying to use TextInputLayout in design library and I added support libraries as following:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.google.android.gms:play-services:4.0.30'
    compile "com.android.support:support-v4:+"
    compile 'com.squareup.picasso:picasso:2.3.2'
    compile 'com.nineoldandroids:library:2.4.0'
    compile 'com.daimajia.slider:library:1.1.5@aar'
    compile 'com.navercorp.pulltorefresh:library:3.2.0@aar'
    compile fileTree(dir: 'libs', include: 'Parse-*.jar')
    compile fileTree(dir: 'libs', include: 'ParseCrashReporting-*.jar')
    compile 'uk.co.chrisjenx:calligraphy:2.1.0'
    compile 'com.android.support:design:22.2.1'
}

However, it gives me following error. What might be the cause of it?

E/AndroidRuntime﹕ FATAL EXCEPTION: main
        android.view.InflateException: Binary XML file line #12: Error inflating class android.support.design.widget.TextInputLayout
                at android.view.LayoutInflater.createView(LayoutInflater.java:613)
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
                at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                at com.myproject.fragments.LostPropertyCheckFragment.onCreateView(LostPropertyCheckFragment.java:48)
                at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
                at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
                at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
                at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
                at android.os.Handler.handleCallback(Handler.java:615)
                at android.os.Handler.dispatchMessage(Handler.java:92)
                at android.os.Looper.loop(Looper.java:137)
                at android.app.ActivityThread.main(ActivityThread.java:4921)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.reflect.InvocationTargetException
                at java.lang.reflect.Constructor.constructNative(Native Method)
                at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
                at android.view.LayoutInflater.createView(LayoutInflater.java:587)
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
                at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                at com.myproject.fragments.LostPropertyCheckFragment.onCreateView(LostPropertyCheckFragment.java:48)
                at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
                at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
                at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
                at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
                at android.os.Handler.handleCallback(Handler.java:615)
                at android.os.Handler.dispatchMessage(Handler.java:92)
                at android.os.Looper.loop(Looper.java:137)
                at android.app.ActivityThread.main(ActivityThread.java:4921)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
                at android.content.res.TypedArray.getColor(TypedArray.java:326)
                at android.support.design.widget.CollapsingTextHelper.setCollapsedTextAppearance(CollapsingTextHelper.java:166)
                at android.support.design.widget.TextInputLayout.<init>(TextInputLayout.java:124)
                at java.lang.reflect.Constructor.constructNative(Native Method)
                at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
                at android.view.LayoutInflater.createView(LayoutInflater.java:587)
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
                at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                at com.myproject.fragments.LostPropertyCheckFragment.onCreateView(LostPropertyCheckFragment.java:48)
                at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
                at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
                at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
                at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
                at android.os.Handler.handleCallback(Handler.java:615)
                at android.os.Handler.dispatchMessage(Handler.java:92)
                at android.os.Looper.loop(Looper.java:137)
                at android.app.ActivityThread.main(ActivityThread.java:4921)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
                at dalvik.system.NativeStart.main(Native Method)

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="70dp"
    android:background="@color/white"
    android:clickable="true"
    android:orientation="vertical" >

    <android.support.design.widget.TextInputLayout
        android:id="@+id/text_input_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/trackno"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/track_no"
            android:inputType="number" />
    </android.support.design.widget.TextInputLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right|center_vertical"
        android:layout_marginTop="5dp">

        <Button
            android:id="@+id/check"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@color/fav_light_green"
            android:gravity="center"
            android:padding="5dp"
            android:text="@string/check"
            android:textColor="@color/white" >
        </Button>
    </LinearLayout>

</LinearLayout>

fragment

public class LostPropertyCheckFragment extends Fragment {

    EditText trackno;
    Button check;
    Dialog dialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get back arguments
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Defines the xml file for the fragment
        View view = inflater.inflate(R.layout.lostpropertycheck_fragment,
                container, false);

        trackno = (EditText) view.findViewById(R.id.trackno);
        check = (Button) view.findViewById(R.id.check);

        check.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (trackno.getText().toString().trim().equalsIgnoreCase("")) {
                    Toast.makeText(
                            getActivity(),
                            getActivity().getResources().getString(
                                    R.string.formwarning), Toast.LENGTH_LONG)
                            .show();
                }

                else {
                    new checkLostProperty().execute();
                }

            }
        });

        return view;
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getActivity()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager
                .getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }

    public static LostPropertyCheckFragment newInstance() {
        LostPropertyCheckFragment fragment = new LostPropertyCheckFragment();

        return fragment;
    }

styles

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.

    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.

        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
        <!--<item name="android:textViewStyle">@style/AppTheme.Widget.TextView</item>-->
       <!-- <item name="textFieldStyle">@style/IconTextViewStyle</item>-->
    </style>

    <style name="IconTextViewStyle"
        parent="android:Widget.TextView">
        <item name="fontPath">fonts/icomoon.ttf</item>
    </style>

    <attr name="textFieldStyle"
        format="reference" />



    <!--<style name="AppTheme.Widget"/>

    <style name="AppTheme.Widget.TextView" parent="android:Widget.Holo.Light.TextView">
        <item name="fontPath">fonts/opensansregular.ttf</item>
    </style>-->

</resources>

回答1:


Without change anything in my gradle I simple fixed my issue by adding:

app:hintTextAppearance="@android:style/TextAppearance.Medium"

I hope this can help someone




回答2:


You should try few things to fix the problem.

  • extend your activity from AppCompatActivity
  • extend your fragment from android.support.v4.app.Fragment (you will need to add support library in build.gradle file, if not added).
  • You should use 22.2.1 or later version of design library (at the time of posting, latest version is 'com.android.support:design:23.0.0') for correct 'hint' behaviour on the lollipop and above devices. If you will use lower version than 22.2.1 , you will face issue discussed on stackOverflow. (In your case you are already using 22.2.1).
  • Instead of using EditText, use android.support.v7.widget.AppCompatEditText.

    • For example:

      <android.support.design.widget.TextInputLayout
                      android:id="@+id/tl_firstName"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      app:errorEnabled="true">
      
                      <android.support.v7.widget.AppCompatEditText
                          android:id="@+id/editText_firstName"
                          style="@style/editProfile_editText"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:hint="First Name"
                          android:inputType="textPersonName"
                          android:singleLine="true" />
      
      </android.support.design.widget.TextInputLayout>
      


来源:https://stackoverflow.com/questions/31566152/android-support-design-widget-textinputlayout-gives-inflateexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!