我正在使用Xamarin表单(PCL)构建跨平台应用程序。 一切都很好,但是当我使用TabbedPage并添加子项时,我在Android项目的MainActivity.cs中得到以下异常:
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c] in < 896ad1d315ca4ba7b117efb8dacaedcf >:0
at Java.Interop.JniEnvironment + InstanceMethods.CallVoidMethod(Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue * args)[0x00069] in < 6cd960837cc24c26bab2a0a29b597627 >:0
at Android.Runtime.JNIEnv.CallVoidMethod(System.IntPtr jobject, System.IntPtr jmethod, Android.Runtime.JValue * parms)[0x0000e] in < 952e88faf9b94435845932f104eb293a >:0
at Android.Support.Design.Widget.TabLayout.SetupWithViewPager(Android.Support.V4.View.ViewPager viewPager)[0x00060] in < 12ef52c9fdb344f58cbdf75db3fa86d8 >:0
at Xamarin.Forms.Platform.Android.AppCompat.TabbedPageRenderer.OnChildrenCollectionChanged(System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)[0x00085] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\TabbedPageRenderer.cs:273
at Xamarin.Forms.Platform.Android.AppCompat.TabbedPageRenderer.OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs`1[TElement] e)[0x00114] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\TabbedPageRenderer.cs:175
at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetElement(TElement element)[0x000f4] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:196
at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement(Xamarin.Forms.VisualElement element)[0x00027] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:137
at Xamarin.Forms.Platform.Android.Platform.CreateRenderer(Xamarin.Forms.VisualElement element)[0x0001f] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Platform.cs:289
at Xamarin.Forms.Platform.Android.AppCompat.Platform.AddChild(Xamarin.Forms.Page page, System.Boolean layout)[0x00015] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:250
at Xamarin.Forms.Platform.Android.AppCompat.Platform.SetPage(Xamarin.Forms.Page newRoot)[0x00089] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:239
at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.InternalSetPage(Xamarin.Forms.Page page)[0x0009b] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\FormsAppCompatActivity.cs:382
at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.SetMainPage()[0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\FormsAppCompatActivity.cs:452
at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.LoadApplication(Xamarin.Forms.Application application)[0x00183] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\FormsAppCompatActivity.cs:136
at App1.Droid.MainActivity.OnCreate(Android.OS.Bundle bundle)[0x000ac] in ........Visual Studio 2017\Projects\App1\App1\App1.Android\MainActivity.cs:42
-- - End of managed Java.Lang.ClassCastException stack trace-- -
java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.widget.ImageView
at android.support.design.widget.TabLayout$TabView.update(TabLayout.java:1531)
at android.support.design.widget.TabLayout$TabView.setTab(TabLayout.java:1484)
at android.support.design.widget.TabLayout$TabView.access$700(TabLayout.java:1348)
at android.support.design.widget.TabLayout.createTabView(TabLayout.java:799)
at android.support.design.widget.TabLayout.newTab(TabLayout.java:500)
at android.support.design.widget.TabLayout.populateFromPagerAdapter(TabLayout.java:773)
at android.support.design.widget.TabLayout.setPagerAdapter(TabLayout.java:764)
at android.support.design.widget.TabLayout.setupWithViewPager(TabLayout.java:716)
at md58adf3f2592a7497b73592b5e766304c2.MainActivity.n_onCreate(Native Method)
at md58adf3f2592a7497b73592b5e766304c2.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.- wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
MainActivity.cs
[Activity(Label = "App1", Icon = "@drawable/icon",
Theme = "@style/MainTheme", MainLauncher = false,
ConfigurationChanges = ConfigChanges.ScreenSize |
ConfigChanges.Orientation)]
public class MainActivity :
global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
#region Resolver Init
SimpleContainer container = new SimpleContainer();
container.Register<IDevice>(t => AndroidDevice.CurrentDevice);
container.Register<IDisplay>(t => t.Resolve<IDevice>().Display);
container.Register<INetwork>(t => t.Resolve<IDevice>().Network);
Resolver.SetResolver(container.GetResolver());
#endregion
global::Xamarin.Forms.Forms.Init(this, bundle);
try
{
LoadApplication(new App());
}
catch (Exception ex)
{
}
}
}
App.cs
public App()
{
InitializeComponent();
MainPage = new HomeScreenTabbed()
{
BarBackgroundColor = Color.FromHex("#5C8BF6")
};
}
HomeScreenTabbed.cs
public HomeScreenTabbed()
{
InitializeComponent();
Xamarin.Forms.NavigationPage.SetHasNavigationBar(this, false);
try
{
Children.Add(new ListViewPage1());
Children.Add(new TabbedPageFirst());
}
catch (Exception ex)
{
Device.BeginInvokeOnMainThread(() =>
{
DisplayAlert("Exception in RelativeLayout.Children.Add
CreateUserSection", ex.ToString(), "ok");
});
}
}
当2个添加儿童行被推荐时,没有问题。 ListViewPage1和TabbedPageFirst不包含任何代码,而不是创建它们时的代码。 测试在模拟器Android 6.0 - API 23上执行。 Visual Studio Enterprise 2017 15.3.3
有什么想法吗?
非常感谢!
答案 0 :(得分:0)
像这样在App.Cs文件中进行更改
public App()
{
InitializeComponent();
MainPage =new NavigationPage(new HomeScreenTabbed())
{
BarBackgroundColor = Color.FromHex("#5C8BF6")
};
}