引用Android Design包出現的錯誤

來源:互聯網
上載者:User

標籤:android studio   tablayout   

在用Android Studio開發的過程中,一遇到廢棄、不被推薦的方法和類,我就想做點什麼去掉上面的橫線。然後,被一個不是問題的問題困擾了很久。

之前我們在建立固定Tabs的時候,類似於這樣的功能,

使用的是ActionBar.Tab但是ActionBar deprecated。那什麼是新的解決方案呢?TabLayout+ViewPager就能實現上述的功能

出現的錯誤:android.view.InflateException: Binary XML file line #8: Error inflating class android.support.design

首先,我明確一下,我的sdk升級到了21版本以上。support Library 也是最新的。

在Android studio添加依賴

build.gradle(Module.app)

dependencies {    compile fileTree(dir: 'libs', include: ['*.jar'])    compile 'com.android.support:appcompat-v7:22.2.0'    compile 'com.android.support:support-v4:22.2.0'    compile 'com.android.support:design:22.2.0'}
activity_main.xml內容如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <android.support.design.widget.TabLayout        android:id="@+id/sliding_tabs"        android:layout_width="match_parent"        android:layout_height="wrap_content"        app:tabMode="scrollable" />    <android.support.v4.view.ViewPager        android:id="@+id/viewpager"        android:layout_width="match_parent"        android:layout_height="0px"        android:layout_weight="1"        android:background="@android:color/white" /></LinearLayout>

錯誤資訊提示:

Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class android.support.design.widget.TabLayout        at android.view.LayoutInflater.createView(LayoutInflater.java:633)        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)        at android.app.Activity.setContentView(Activity.java:2144)        at com.example.hp.myapplication.MainActivity.onCreate(MainActivity.java:15)        at android.app.Activity.performCreate(Activity.java:5933)
上面提示的錯誤是,在xml檔案中的第8行,出現了Inflate錯誤,然後導致MainActivity.java:15行中的

setContentView(R.layout.activity_main);
錯誤。

解決錯誤的方法:

在res/values中的styles.xml中,需要修改parent的屬性值,不能使用自訂的。

這裡面有兩個styles.xml都需要修改

修改內容,參考如下:

<resources>    <style name="AppTheme" parent="Base.AppTheme">        <!-- Customize your theme here. -->    </style>    <!-- Base application theme. -->    <style name="Base.AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">        <item name="colorPrimary">#673AB7</item>        <item name="colorPrimaryDark">#512DA8</item>        <item name="colorAccent">#FF4081</item>    </style></resources>
修改parent中的屬性值,與上面定義的對應
<resources>    <style name="AppTheme" parent="Base.AppTheme">    </style></resources>

結果就成功解決了。

類似問題參考連結:http://stackoverflow.com/questions/30547323/error-when-using-any-android-design-support-library-elements/30557995#30557995



引用Android Design包出現的錯誤

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.