Tabs崩溃在Android版本下面的棒棒糖应用程序

时间:2017-08-04 10:57:44

标签: android xamarin.android android-viewpager android-tabs

我有一个Tabs设置的活动,标签使用视图寻呼机工作,这就是问题,它适用于棒棒糖,棉花糖和Nougat,但任何低于5.0的应用程序崩溃,我不会'知道可能导致此错误的原因。 这是我的主要活动布局,其中包含标签:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@android:style/ThemeOverlay.Material.Light">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:titleTextColor="@android:color/black"
            app:titleTextAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
            app:layout_scrollFlags="scroll|enterAlways|snap" />
        <include
            android:id="@+id/tabs"
            layout="@layout/tabs" />
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/mainFAB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:clickable="true"
        android:layout_gravity="bottom|end"
        app:fabSize="normal"
        app:srcCompat="@drawable/ic_edit" />
</android.support.design.widget.CoordinatorLayout>

以及我的OnCreate方法:

protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            _adapter = new TabsAdapter(this, SupportFragmentManager);
            _pager = FindViewById<ViewPager>(Resource.Id.viewpager);
            var tabs = FindViewById<TabLayout>(Resource.Id.tabs);

            _pager.Adapter = _adapter;
            tabs.SetupWithViewPager(_pager);

            _pager.OffscreenPageLimit = 5;

            SupportActionBar.SetDisplayHomeAsUpEnabled(false);
            SupportActionBar.SetHomeButtonEnabled(false);
        }

我正在使用Xamarin.Android。这是错误:

Android.Views.InflateException: Binary XML file line #1: Error inflating class android.support.design.widget.TabLayout

错误输出:

[ERROR] FATAL UNHANDLED EXCEPTION: Android.Views.InflateException: Binary XML file line #1: Error inflating class android.support.design.widget.TabLayout ---> Java.Lang.Reflect.InvocationTargetException: Exception of type 'Java.Lang.Reflect.InvocationTargetException' was thrown. ---> Java.Lang.UnsupportedOperationException: Can't convert to color: type=0x2
08-04 13:06:47.696 E/mono-rt ( 1291):    --- End of inner exception stack trace ---
08-04 13:06:47.696 E/mono-rt ( 1291):    --- End of inner exception stack trace ---
08-04 13:06:47.696 E/mono-rt ( 1291):   --- End of managed Android.Views.InflateException stack trace ---
08-04 13:06:47.696 E/mono-rt ( 1291): android.view.InflateException: Binary XML file line #1: Error inflating class android.support.design.widget.TabLayout
08-04 13:06:47.696 E/mono-rt ( 1291):   at android.view.LayoutInflater.createView(LayoutInflater.java:620)
08-04 13:06:47.696 E/mono-rt ( 1291):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
08-04 13:06:47.696 E/mono-rt ( 1291):   at android.view.LayoutInflater.parseInclude(LayoutInflater.java:816)
08-04 13:06:47.696 E/mono-rt ( 1291):   at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
08-04 13:06:47.696 E/mono-rt ( 1291):   at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
08-04 13:06:47.696 E/mono-rt ( 1291):   at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
08-04 13:06:47.696 E/mono-rt ( 1291):   at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
08-04 13:06:47.696 E/mono-rt ( 1291):   at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
08-04 13:06:47.696 E/mono-rt ( 1291):   at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
08-04 13:06:47.696 E/mono-rt ( 1291):   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
08-04 13:06:47.696 E/mono-rt ( 1291):   at md52271f797d1fff0147ee94ae02da80c1d.MainActivity.n_onCreate(Native Method)
08-04 13:06:47.696 E/mono-rt ( 1291):   at md52271f797d1fff0147ee94ae02da80c1d.MainActivity.onCreate(MainActivity.java:31)
08-04 13:06:47.696 E/mono-rt ( 1291):   at android.app.Activity.performCreate(Activity.java:5231)
08-04 13:06:47.696 E/mono-rt ( 1291):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-04 13:06:47.696 E/mono-rt ( 1291):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
08-04 13:06:47.696 E/mono-rt ( 1291):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
08-04 13:06:47.696 E/mono-rt ( 1291):   at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-04 13:06:47.696 E/mono-rt ( 1291):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-04 13:06:47.696 E/mono-rt ( 1291):   at android.os.Handler.dispatchMessage.Constructor.constructNative(Native Method)
08-04 13:06:47.696 E/mono-rt ( 1291):   at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
08-04 13:06:47.696 E/mono-rt ( 1291):   at android.view.LayoutInflater.createView(LayoutInflater.java:594)(Handler.java:102)
08-04 13:06:47.696 E/mono-rt ( 1291):   at android.os.Looper.loop(Looper.java:136)
08-04 13:06:47.696 E/mono-rt ( 1291):   at android.app.ActivityThread.main(ActivityThread.java:5001)
08-04 13:06:47.696 E/mono-rt ( 1291):   at java.lang.reflect.Method.invokeNative(Native Method)
08-04 13:06:47.696 E/mono-rt ( 1291):   at java.lang.reflect.Method.invoke(Method.java:515)
08-04 13:06:47.696 E/mono-rt ( 1291):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-04 13:06:47.696 E/mono-rt ( 1291):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-04 13:06:47.696 E/mono-rt ( 1291):   at dalvik.system.NativeStart.main(Native Method)
08-04 13:06:47.696 E/mono-rt ( 1291): Caused by: java.lang.reflect.InvocationTargetException
08-04 13:06:47.696 E/mono-rt ( 1291):   at java.lang.reflect

2 个答案:

答案 0 :(得分:0)

I believe the problem is android:background="?attr/colorPrimary". I think that was introduced in Android 5.0. You could either hard code a @color/foo from your color palette or set up styles specific to Android versions in your styles.xml.

答案 1 :(得分:0)

最后绕过它,旁边是attr?功能,这也是一个问题

button.setBackgroundImage(UIImage.init(named: "image"), for: .normal)
button.titleLabel?.textAlignment = .left

在主题属性中,我将其更改为

<android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@android:style/ThemeOverlay.Material.Light">

不知道它为什么会起作用,但这使它起作用。如果有人能解释为什么它有效,我会很高兴。