I'm trying to setup tabs like Google Play, I'm following this guide and converting to C#, but an exception is thrown when calling tabLayout.SetupWithViewPager
. The code for the activity is this:
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);
}
}
The stacktrace for the exception is:
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)
I'm not using <merge />
anywhere so I presume that it is internal to one of the controls? I presume that this is just an issue with Xamarin Android (or I've made a mistake somewhere) as there seems to be several very similar Java implementations that I assume work ok!
I've uploaded the full sample to GitHub.
答案 0 :(得分:34)
在清除bin和obj目录后,它会出现幻像错误,现在可以正常工作了。
答案 1 :(得分:1)
出于其他原因,我有同样的问题,如果您包含任何嵌套片段,请不要实现onCreateview()
因此,如果您在viewPager中包含嵌套片段,请确保它们都以正确的方式实现
希望这能激发任何人面对同样的问题