简体   繁体   English

尝试调用新的 Fragment 页面时活动被破坏 - Xamarin.Android

[英]Activity Destroyed when trying to call a new Fragment page - Xamarin.Android

I am using a Xamarin.Android template (BottomNavigationView) to create an app with a bottom menu.我正在使用 Xamarin.Android 模板 (BottomNavigationView) 创建带有底部菜单的应用程序。 I am trying to get it so that one of the pages that is on the menu ie a profile page has a button that will take me to another fragment.我试图得到它,以便菜单上的页面之一,即个人资料页面有一个按钮,可以将我带到另一个片段。 However when I call the SupportFragmentManager I am getting an error saying "the activity has been destroyed".但是,当我调用 SupportFragmentManager 时,我收到一条错误消息,指出“活动已被破坏”。

在此处输入图片说明

I'd like both those buttons to go to other pages that also display the bottom menu button (as i would like it to be consistent throughout my app).我希望这两个按钮都可以转到也显示底部菜单按钮的其他页面(因为我希望它在整个应用程序中保持一致)。 The code I am using is:我正在使用的代码是:

FragmentActivity fragmentActivity = new FragmentActivity();
               fragmentActivity.SupportFragmentManager.BeginTransaction()
               .Replace(Resource.Id.content_frame, ShowFriendRequests.NewInstance())
               .Commit();

The template I am using uses the android.support.v4.app.Fragment and has a MainActivity that I think uses FragmentActivity.我使用的模板使用 android.support.v4.app.Fragment 并且有一个我认为使用 FragmentActivity 的 MainActivity。 But I'm not entirely sure because it was all premade.但我不完全确定,因为它都是预制的。 I have just added the fragment files.我刚刚添加了片段文件。

Any idea why I'd be getting this error?知道为什么我会收到这个错误吗?

Thank you谢谢

Anyway I think you are not preforming FragmentTransaction correctly instead of using FragmentActivity use FragmentTransaction to replace one Fragment with another or to Add another Fragment .无论如何,我认为您没有正确执行FragmentTransaction而不是使用FragmentActivity使用FragmentTransaction将一个Fragment替换为另一个FragmentAdd另一个Fragment For example in Java code that would go like this:例如,在Java代码中会像这样:

FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.content_frame, new ShowFriendRequests()).commit();

In c# I suppose something like this:在 C# 中,我想是这样的:

FragmentTransaction fragmentTx = FragmentManager.BeginTransaction();
ShowFriendRequests friendRequest = new ShowFriendRequests();

// Id is ID of your layout which you want to replace with fragment
fragmentTx.Replace(Resource.Id.content_frame, friendRequest);
fragmentTx.Commit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM