Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin内存泄漏_Xamarin - Fatal编程技术网

Xamarin内存泄漏

Xamarin内存泄漏,xamarin,Xamarin,我是Xamarin的新手。我创建了一个使用DroperLayout(Android)的应用程序。但我的问题是,每次我在菜单(DroperLayout菜单)中选择一个项目时,内存就会增加,这会导致应用程序变得缓慢和崩溃。我曾尝试使用Xamarin profiler来分析内存泄漏-它怀疑是String.FastAllocationString,但它并没有真正显示导致String.FastAllocationString问题的行(代码)。请帮忙?这是我的密码: main活动 DrawerLayout

我是Xamarin的新手。我创建了一个使用DroperLayout(Android)的应用程序。但我的问题是,每次我在菜单(DroperLayout菜单)中选择一个项目时,内存就会增加,这会导致应用程序变得缓慢和崩溃。我曾尝试使用Xamarin profiler来分析内存泄漏-它怀疑是String.FastAllocationString,但它并没有真正显示导致String.FastAllocationString问题的行(代码)。请帮忙?这是我的密码:

main活动

DrawerLayout drawerLayout;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);

            // Init toolbar
            var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.app_bar);
            SetSupportActionBar(toolbar);
            SupportActionBar.SetTitle(Resource.String.app_name);
            SupportActionBar.SetDisplayHomeAsUpEnabled(true);
            SupportActionBar.SetDisplayShowHomeEnabled(true);

            // Attach item selected handler to navigation view
            var navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
            navigationView.NavigationItemSelected += NavigationView_NavigationItemSelected;

            // Create ActionBarDrawerToggle button and add it to the toolbar
            var drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, Resource.String.open_drawer, Resource.String.close_drawer);
            drawerLayout.SetDrawerListener(drawerToggle);
            drawerToggle.SyncState();
        }

void NavigationView_NavigationItemSelected(object sender, NavigationView.NavigationItemSelectedEventArgs e)
{
    var ft = FragmentManager.BeginTransaction();
    ft.AddToBackStack(null);

    switch (e.MenuItem.ItemId)
    {
        case (Resource.Id.nav_incidents):
            SupportActionBar.SetTitle(Resource.String.toolbar_Test);
            ft.Add(Resource.Id.HomeFrameLayout, new Test());
            break;
    }

    ft.Commit();

    ft.Dispose();

    // Close drawer
    drawerLayout.CloseDrawers();
}
Xamarin分析器


在添加新片段之前,必须检查片段是否可用

switch (e.MenuItem.ItemId)
{
   case (Resource.Id.nav_incidents):
        SupportActionBar.SetTitle(Resource.String.toolbar_Test);
        Fragment myFragment = 
        (Fragment)FragmentManager.FindFragmentByTag("FRAGMENT1");
        if (myFragment.IsVisible){
           ft.Replace(Resource.Id.HomeFrameLayout, new Test(),"FRAGMENT1");
        }else{
           ft.Add(Resource.Id.HomeFrameLayout, new Test(),"FRAGMENT1");
        }
        break;
}

希望此帮助

当它崩溃时,
logcat
中的输出是什么,因为我没有看到1/2MB字符串是您所显示的原因。(我确实看到您在创建片段时没有检查片段是否已经存在)。这产生了巨大的影响,解决了我的问题。非常感谢。@chosenOneThabs您的好消息,这个社区是互相帮助的,我很高兴我能帮助您解决您的问题。。。
switch (e.MenuItem.ItemId)
{
   case (Resource.Id.nav_incidents):
        SupportActionBar.SetTitle(Resource.String.toolbar_Test);
        Fragment myFragment = 
        (Fragment)FragmentManager.FindFragmentByTag("FRAGMENT1");
        if (myFragment.IsVisible){
           ft.Replace(Resource.Id.HomeFrameLayout, new Test(),"FRAGMENT1");
        }else{
           ft.Add(Resource.Id.HomeFrameLayout, new Test(),"FRAGMENT1");
        }
        break;
}