Xamarin Android TabLayout调用SetupWithViewPager抛出异常
本文关键字:SetupWithViewPager 抛出异常 调用 TabLayout Android Xamarin | 更新日期: 2023-09-27 18:05:26
我正在尝试设置像Google Play这样的选项卡,我遵循本指南并转换为c#,但是在调用tabLayout.SetupWithViewPager
时抛出异常。该活动的代码如下:
public class MainActivity : FragmentActivity
{
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
var viewPager = FindViewById<ViewPager>(Resource.Id.viewpager);
viewPager.Adapter = new SampleFragmentPagerAdapter(SupportFragmentManager, this);
var tabLayout = FindViewById<TabLayout>(Resource.Id.sliding_tabs);
tabLayout.SetupWithViewPager(viewPager);
}
}
异常的堆栈跟踪是:
07-28 23:08:39.068 I/MonoDroid( 4040): UNHANDLED EXCEPTION:
07-28 23:08:39.071 I/MonoDroid( 4040): Android.Views.InflateException: Exception of type 'Android.Views.InflateException' was thrown.
07-28 23:08:39.071 I/MonoDroid( 4040): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x0004b>
07-28 23:08:39.071 I/MonoDroid( 4040): at Android.Runtime.JNIEnv.CallVoidMethod (intptr,intptr,Android.Runtime.JValue*) [0x00063] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:571
07-28 23:08:39.071 I/MonoDroid( 4040): at Android.Support.Design.Widget.TabLayout.SetupWithViewPager (Android.Support.V4.View.ViewPager) <IL 0x00066, 0x001c3>
07-28 23:08:39.071 I/MonoDroid( 4040): at TabTest.MainActivity.OnCreate (Android.OS.Bundle) [0x00040] in E:'VS Projects'TabTest'TabTest'MainActivity.cs:28
07-28 23:08:39.071 I/MonoDroid( 4040): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.App.Activity.cs:2707
07-28 23:08:39.071 I/MonoDroid( 4040): at (wrapper dynamic-method) object.1f96a184-9cb6-4d05-8516-4c4d2e85d8d7 (intptr,intptr,intptr) <IL 0x00017, 0x00027>
07-28 23:08:39.071 I/MonoDroid( 4040): --- End of managed exception stack trace ---
07-28 23:08:39.071 I/MonoDroid( 4040): android.view.InflateException: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true
07-28 23:08:39.071 I/MonoDroid( 4040): at android.view.LayoutInflater.inflate(LayoutInflater.java:475)
07-28 23:08:39.071 I/MonoDroid( 4040): at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
07-28 23:08:39.071 I/MonoDroid( 4040): at android.support.design.widget.TabLayout$TabView.update(TabLayout.java:1196)
07-28 23:08:39.071 I/MonoDroid( 4040): at android.support.design.widget.TabLayout$TabView.<init>(TabLayout.java:1112)
07-28 23:08:39.071 I/MonoDroid( 4040): at android.support.design.widget.TabLayout.createTabView(TabLayout.java:612)
07-28 23:08:39.071 I/MonoDroid( 4040): at android.support.design.widget.TabLayout.addTabView(TabLayout.java:646)
07-28 23:08:39.071 I/MonoDroid( 4040): at android.support.design.widget.TabLayout.addTab(TabLayout.java:345)
07-28 23:08:39.071 I/MonoDroid( 4040): at android.support.design.widget.TabLayout.addTab(TabLayout.java:320)
07-28 23:08:39.071 I/MonoDroid( 4040): at android.support.design.widget.TabLayout.setTabsFromPagerAdapter(TabLayout.java:601)
07-28 23:08:39.071 I/MonoDroid( 4040): at android.support.design.widget.TabLayout.setupWithViewPager(TabLayout.java:575)
07-28 23:08:39.071 I/MonoDroid( 4040): at md5be39043e4dbb7988fedb01f9386df4d7.MainActivity.n_onCreate(Native Method)
07-28 23:08:39.071 I/MonoDroid( 4040): at md5be39043e4dbb7988fedb01f9386df4d7.MainActivity.onCreate(MainActivity.java:28)
07-28 23:08:39.071 I/MonoDroid( 4040): at android.app.Activity.performCreate(Activity.java:5990)
07-28 23:08:39.071 I/MonoDroid( 4040): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
07-28 23:08:39.071 I/MonoDroid( 4040): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
07-28 23:08:39.071 I/MonoDroid( 4040): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
07-28 23:08:39.071 I/MonoDroid( 4040): at android.app.ActivityThread.access$800(ActivityThread.java:151)
07-28 23:08:39.071 I/MonoDroid( 4040): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
07-28 23:08:39.071 I/MonoDroid( 4040): at android.os.Handler.dispatchMessage(Handler.java:102)
07-28 23:08:39.071 I/MonoDroid( 4040): at android.os.Looper.loop(Looper.java:135)
07-28 23:08:39.071 I/MonoDroid( 4040): at android.app.ActivityThread.main(ActivityThread.java:5254)
07-28 23:08:39.071 I/MonoDroid( 4040): at java.lang.reflect.Method.invoke(Native Method)
07-28 23:08:39.071 I/MonoDroid( 4040): at java.lang.reflect.Method.invoke(Method.java:372)
07-28 23:08:39.071 I/MonoDroid( 4040): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
07-28 23:08:39.071 I/MonoDroid( 4040): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
我没有在任何地方使用<merge />
,所以我认为它是一个控件的内部?我认为这只是Xamarin Android的一个问题(或者我在某个地方犯了一个错误),因为似乎有几个非常相似的Java实现,我认为工作正常!
我已经将完整的示例上传到GitHub。
清除bin和obj目录后,它现在可以正常工作了。
我有同样的问题,另一个原因,这个问题可能会提出,如果你包括任何嵌套片段不实现onCreateview()
所以如果你在viewPager中包含嵌套片段确保它们都以正确的方式实现
希望这篇文章能激励大家面对同样的问题