错误膨胀类 android.support.design.widget.TabLayout

我试图使用Eclipse创建一个基于Google的“材料设计”的菜单选项卡,但我得到了一个错误:

错误膨胀类 android.support.design.widget.TabLayout

日志文件:

07-30 00:52:40.588: E/Trace(31652): error opening trace file: No such file or directory (2)
07-30 00:52:40.672: E/AndroidRuntime(31652): FATAL EXCEPTION: main
07-30 00:52:40.672: E/AndroidRuntime(31652): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.mykitchen/com.app.mykitchen.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class android.support.design.widget.TabLayout
07-30 00:52:40.672: E/AndroidRuntime(31652):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at android.app.ActivityThread.access$600(ActivityThread.java:149)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at android.os.Looper.loop(Looper.java:153)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at android.app.ActivityThread.main(ActivityThread.java:4987)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at java.lang.reflect.Method.invokeNative(Native Method)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at java.lang.reflect.Method.invoke(Method.java:511)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at dalvik.system.NativeStart.main(Native Method)
07-30 00:52:40.672: E/AndroidRuntime(31652): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class android.support.design.widget.TabLayout
07-30 00:52:40.672: E/AndroidRuntime(31652):    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:258)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at android.app.Activity.setContentView(Activity.java:1867)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at com.app.mykitchen.MainActivity.onCreate(MainActivity.java:30)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at android.app.Activity.performCreate(Activity.java:5020)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
07-30 00:52:40.672: E/AndroidRuntime(31652):    ... 11 more
07-30 00:52:40.672: E/AndroidRuntime(31652): Caused by: java.lang.reflect.InvocationTargetException
07-30 00:52:40.672: E/AndroidRuntime(31652):    at java.lang.reflect.Constructor.constructNative(Native Method)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
07-30 00:52:40.672: E/AndroidRuntime(31652):    ... 22 more
07-30 00:52:40.672: E/AndroidRuntime(31652): Caused by: java.lang.NoClassDefFoundError: android.support.design.R$styleable
07-30 00:52:40.672: E/AndroidRuntime(31652):    at android.support.design.widget.TabLayout.<init>(TabLayout.java:236)
07-30 00:52:40.672: E/AndroidRuntime(31652):    at android.support.design.widget.TabLayout.<init>(TabLayout.java:221)
07-30 00:52:40.672: E/AndroidRuntime(31652):    ... 25 more

主要活动.xml:

<?xml version="1.0" encoding="utf-8"?>
<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"
/>
<android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:background="@color/white" />

</LinearLayout>

我尝试将 AppBase 主题设置为 AppCompat,但这无济于事。


答案 1

不知道这是否与您的问题有关。我将 TabLayout 和 ViewPager 的标签更改为与此页面上的标签对应的标签:https://material.io/develop/android/components/tab-layout/

这是我的应用程序的代码,它使错误消失了:

<com.google.android.material.tabs.TabLayout
    android:id="@+id/sliding_tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabMode="fixed"/>

<androidx.viewpager.widget.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="1"
    android:background="@android:color/white" />

答案 2

我的Logcat中还有更多。最后你可以看到,有明确的解释:

You need to use a Theme.AppCompat theme (or descendant) with the design library.

android.view.inflateException: Binary XML 文件行 #6: 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.rInflater(LayoutInflater.java:806) at android.view.LayoutInflater.inflate(LayoutInflater.java:504) at android.view.LayoutInflater.inflate(LayoutInflater.java:414) at pl.acme_gliwice.smieciarka.nowe.ekran_glowny。MainTrasyFragment.onCreateView(MainTrasyFragment.java:44) at android.support.v4.app.Fragment.performCreateView(Fragment.java:2337) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1418) at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1739) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1808) at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799) at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2579) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2366) at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2321) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2228) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:699) at android.os.Handler.handleCallback(Handler.java:739) at android.os.handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5376) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908) at com.android.internal.os.ZygoteInit.main(合格特初始化.java:703)Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:288) at android.view.LayoutInflater.createView(LayoutInflater.java:607) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at android.view.LayoutInflater.inflate(LayoutInflater.java:504) atandroid.view.LayoutInflater.inflate(LayoutInflater.java:414) at pl.acme_gliwice.smieciarka.nowe.ekran_glowny.MainTrasyFragment.onCreateView(MainTrasyFragment.java:44) at android.support.v4.app.Fragment.performCreateView(Fragment.java:2337) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1418) at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1739) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1808) at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799) at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2579) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2366) at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2321) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2228) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:699) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5376) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703) Caused by: java.lang.IllegalArgumentException: You need use a Theme.AppCompat theme (or descendant)设计库。at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:33) at android.support.design.widget.TabLayout.(TabLayout.java:298) at android.support.design.widget.TabLayout.(选项卡布局.java:292)

上面所有这些风格技巧的例子都不完全正确。

我们有 2 种方案:

1)我们的选项卡布局在活动中。如果是这种情况,我们需要将此活动的主题设置为 AppCompat 主题。首先,我们需要在风格上定义这样的主题.xml(它不必是21版本)。

<style name="TabAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">        
</style>

然后,我们可以在清单文件中定义活动主题。

<activity
    android:name=".MyTabActivity"
    android:theme="TabAppTheme"
/>

我们不需要对布局执行任何操作.xml

2)我们的选项卡布局在片段内部

类似的情况,但很难改变主题。

首先,我们定义主题如上。然后,我们需要更改TabFragment的主题。为此,我们的活动截止日期必须不要在清单中设置主题。它可以从应用程序主题继承它,但不能直接设置它。

然后,我们必须在 OnCreateView 中更改此片段的片段主题:

final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.TabAppTheme);
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
View view = localInflater.inflate(R.layout.fragment_main_trasy, container, false);

创建视图上的整个片段可能如下所示:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // create ContextThemeWrapper from the original Activity Context with the custom theme
    final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.TabAppTheme);
    // clone the inflater using the ContextThemeWrapper
    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);

    View view = localInflater.inflate(R.layout.fragment_main_trasy, container, false);
    tabLayout = (TabLayout) view.findViewById(R.id.tabs);
    viewPager = (ViewPager) view.findViewById(R.id.pager);
    mAdapter = new TabFragment.MyAdapter(getChildFragmentManager());
    viewPager.setAdapter(mAdapter);
    tabLayout.setupWithViewPager(viewPager);

    return view;
}

推荐