0

Please help me with this! I tried this online tutorial (http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/) and it works for a separate test project, but when I integrate the code into my own project, I got errors as below:

05-07 13:17:32.552 4094-4094/com.example.apple.coupletone E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.example.apple.coupletone, PID: 4094
                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.apple.coupletone/com.example.apple.coupletone.MainActivity}: android.view.InflateException: Binary XML file line #21: Binary XML file line #21: Error inflating class android.support.design.widget.TabLayout
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:148)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                         Caused by: android.view.InflateException: Binary XML file line #21: Binary XML file line #21: Error inflating class android.support.design.widget.TabLayout
                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
                                                                            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                                                                            at com.example.apple.coupletone.MainActivity.onCreate(MainActivity.java:27)
                                                                            at android.app.Activity.performCreate(Activity.java:6237)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:148) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                         Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class android.support.design.widget.TabLayout
                                                                            at android.view.LayoutInflater.createView(LayoutInflater.java:645)
                                                                            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
                                                                            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                            at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                                                                            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                            at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
                                                                            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276) 
                                                                            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                                                                            at com.example.apple.coupletone.MainActivity.onCreate(MainActivity.java:27) 
                                                                            at android.app.Activity.performCreate(Activity.java:6237) 
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:148) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                         Caused by: java.lang.reflect.InvocationTargetException
                                                                            at java.lang.reflect.Constructor.newInstance(Native Method)
                                                                            at android.view.LayoutInflater.createView(LayoutInflater.java:619)
                                                                            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
                                                                            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
                                                                            at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
                                                                            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                            at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
                                                                            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276) 
                                                                            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                                                                            at com.example.apple.coupletone.MainActivity.onCreate(MainActivity.java:27) 
                                                                            at android.app.Activity.performCreate(Activity.java:6237) 
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:148) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                         Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x10
                                                                            at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:666)
                                                                            at android.support.design.widget.TabLayout.<init>(TabLayout.java:284)
                                                                            at android.support.design.widget.TabLayout.<init>(TabLayout.java:261)
                                                                            at java.lang.reflect.Constructor.newInstance(Native Method) 
                                                                            at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
                                                                            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
                                                                            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
                                                                            at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
                                                                            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                            at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
                                                                            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276) 
                                                                            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                                                                            at com.example.apple.coupletone.MainActivity.onCreate(MainActivity.java:27) 
                                                                            at android.app.Activity.performCreate(Activity.java:6237) 
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:148) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

My activity_main.xml is like below

    <android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabMode="fixed"
    app:tabGravity="fill"
    style="@style/MyMaterialTheme"/>

My v21/styles.xml file is like below

<resources>

<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">
    <item name="android:windowContentTransitions">true</item>
    <item name="android:windowAllowEnterTransitionOverlap">true</item>
    <item name="android:windowAllowReturnTransitionOverlap">true</item>
    <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
    <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
</style>

and styles.xml

<resources>
<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">

</style>

<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

and part of MainActivity.java

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}

}

and my gradle file is like this

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.google.android.gms:play-services:8.4.0'
compile 'com.android.support:design:23.2.1'
compile 'com.android.support:support-v4:23.2.1'
}

my dimens.xml

<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="tab_max_width">264dp</dimen>
<dimen name="tab_padding_bottom">16dp</dimen>
<dimen name="tab_label">14sp</dimen>
<dimen name="custom_tab_layout_height">72dp</dimen>
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>

Thank you so much for help!

李浥尘
  • 1
  • 2
  • Error says `UnsupportedOperationException: Can't convert to dimension: type=0x10` – OneCricketeer May 07 '16 at 20:06
  • Delete this line `"style="@style/MyMaterialTheme""` and then try to build. Is it working fine? – piotrek1543 May 07 '16 at 20:11
  • Possible duplicate of [Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x1](http://stackoverflow.com/questions/18741485/caused-by-java-lang-unsupportedoperationexception-cant-convert-to-dimension) – piotrek1543 May 07 '16 at 20:13
  • No it doesn't :( This line of code is what I find in another post, but it doesn't work for me. I delete the line and still get InflateException – 李浥尘 May 07 '16 at 20:16
  • I've updated the new error message. Please take a look. Thx – 李浥尘 May 07 '16 at 20:19
  • I have updated my dimension file and please take a look – 李浥尘 May 07 '16 at 20:46
  • I copied your code into a project of my own and did not get an error when running it. Can you post your entire `activity_main.xml` file? – Michiyo May 08 '16 at 21:22

0 Answers0