使用ViewPager和TabLayout与Xamarin时出错
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用ViewPager和TabLayout与Xamarin时出错相关的知识,希望对你有一定的参考价值。
我想在我的应用程序中添加一个点指示器,但是当我在线tabLayout.SetupWithViewPager(viewPager, true);
时出现以下错误:
Java.Lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.ImageView
我已经尝试删除Resource.Designer.cs文件,但这并没有改变任何东西。我在代码中弄乱了什么吗?或者Xamarin每次构建应用程序时都不会重建R.java?该准则在GitHub上线。以下是viewPager所需的主要文件:
Main.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<Toolbar
android:minHeight="?android:attr/actionBarSize"
android:background="?android:attr/colorPrimary"
android:minWidth="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:overScrollMode="never" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.cs:
public class MainActivity : FragmentActivity
{
ViewPager viewPager;
TabLayout tabLayout;
Input Input = new Input();
Graphic Graphic = new Graphic();
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
Toolbar toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
toolbar.SetTitleTextColor(Android.Graphics.Color.White);
SetActionBar(toolbar);
ActionBar.Title = "TyCoDim";
viewPager = (ViewPager) FindViewById(Resource.Id.pager);
tabLayout = (TabLayout) FindViewById(Resource.Id.tabs);
SetupViewPager(viewPager);
tabLayout.SetupWithViewPager(viewPager, true);
viewPager.PageSelected += ViewPager_PageSelected;
}
private void ViewPager_PageSelected(object sender, ViewPager.PageSelectedEventArgs e)
{
}
private void SetupViewPager(ViewPager Pager)
{
Pager.OffscreenPageLimit = 2;
PageAdapter adapter = new PageAdapter(SupportFragmentManager);
adapter.AddFragment(Input, "Input");
adapter.AddFragment(Graphic, "Graphic");
Pager.Adapter = adapter;
}
}
PagerAdapter.cs:
class PageAdapter : FragmentPagerAdapter
{
private readonly List<Fragment> _fragments;
private readonly List<string> _fragmentnames;
public PageAdapter(FragmentManager fm) : base(fm)
{
_fragments = new List<Fragment>();
_fragmentnames = new List<string>();
}
public override int Count
{
get { return _fragments.Count; }
}
public override Fragment GetItem(int position)
{
return _fragments[position];
}
public void AddFragment(Fragment fragment, string name)
{
if (fragment == null) return;
_fragments.Add(fragment);
_fragmentnames.Add(name);
}
public override ICharSequence GetPageTitleFormatted(int position)
{
return new Java.Lang.String(_fragmentnames[position]);
}
}
答案
好的,通过删除obj /文件夹修复它...
以上是关于使用ViewPager和TabLayout与Xamarin时出错的主要内容,如果未能解决你的问题,请参考以下文章
TabLayout与ViewPager和FragmentFragmentPagerAdapter的合用
如何在将 Tablayout 与 viewpager 一起使用时从另一个片段调用 AsyncTask?