J.J
J.J

Reputation: 111

Xamarin The application may be doing too much work on its main thread Android

I have problem with it. When i click button, app instead go to other page with Lists, app is crashing. I have tried add tasks for better performance but error is still on. I don't know what's wrong with that code. For me this should be fine.

Edit: I Have change await Navigation.PushAsync(new SrchPage()); but still it not works. Below i have gotten these errors.

First Error:

The application may be doing too much work on its main thread Android

Second Error:

UNHANDLED EXCEPTION:
10-15 10:13:46.781 I/MonoDroid(27288): 
Xamarin.Forms.Xaml.XamlParseException: No embeddedresource found for 
Ap.SrchPage
10-15 10:13:46.781 I/MonoDroid(27288):   at 
Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.Type 
callingType) [0x0000f] in 
C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlLoader.cs:67 
10-15 10:13:46.781 I/MonoDroid(27288):   at 
Xamarin.Forms.Xaml.Extensions.LoadFromXaml[TXaml] (TXaml view, System.Type 
callingType) [0x00000] in 
C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\ViewExtensions.cs:36 
10-15 10:13:46.781 I/MonoDroid(27288):   at Ap.SrchPage.InitializeComponent 
() [0x00001] in Z:\mem\Ap\Ap\Ap\obj\Debug\Ap.Klasy.SrchPage.xaml.g.cs:22 
10-15 10:13:46.781 I/MonoDroid(27288):   at Ap.SrchPage..ctor () [0x00008] in 
Z:\mem\Ap\Ap\Ap\Klasy\SrchPage.xaml.cs:19 
10-15 10:13:46.781 I/MonoDroid(27288):   at Ap.MainPage+
<Button_Clicked_1>d__6.MoveNext () [0x0000f] in 
Z:\mem\Ap\Ap\Ap\MainPage.xaml.cs:110 
10-15 10:13:46.781 I/MonoDroid(27288): --- End of stack trace from previous 
location where exception was thrown ---
10-15 10:13:46.781 I/MonoDroid(27288):   at 
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in 
<183b200ee49746d48fc781625979428e>:0 
10-15 10:13:46.781 I/MonoDroid(27288):   at 
System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.
<ThrowAsync>b__6_0 (System.Object state) [0x00000] in 
<183b200ee49746d48fc781625979428e>:0 
10-15 10:13:46.781 I/MonoDroid(27288):   at Android.App.SyncContext+
<>c__DisplayClass2_0.<Post>b__0 () [0x00000] in 
<71828dd8fb5f4508815e23d6996c45c2>:0 
10-15 10:13:46.781 I/MonoDroid(27288):   at 
Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in 
 <71828dd8fb5f4508815e23d6996c45c2>:0 
10-15 10:13:46.781 I/MonoDroid(27288):   at Java.Lang.IRunnableInvoker.n_Run 
(System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in 
<71828dd8fb5f4508815e23d6996c45c2>:0 
10-15 10:13:46.781 I/MonoDroid(27288):   at (wrapper dynamic-method) 
System.Object:a8baa1c1-4db1-4761-95e4-cc368e19ffe1 (intptr,intptr)
10-15 10:13:46.801 W/art     (27288): JNI RegisterNativeMethods: attempt to 
register 0 native methods for android.runtime.JavaProxyThrowable

Page with list:

public SrchPage()
{
     InitializeComponent();
     Task.Factory.StartNew(() => Zrob());    
}

    private void Zrob()
     {
       Task.Factory.StartNew(() =>
       {
       ListaNazw.ItemsSource = new List<Listyy>
           {
             new Listyy { nazwa = "tekst", opis="tekst" , ema = 1 },
             new Listyy { nazwa = "tekst", opis = "tekst", ema = 2 },
             new Listyy { nazwa = "tekst", opis = "tekst", ema = 3 },
             new Listyy { nazwa = "tekst", opis = "tekst", ema = 4 },
           };
       });
     }

async  private void ListaNazw_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
     if (e.SelectedItem == null)
                return;

     var klik = e.SelectedItem as Listyy;
     await Navigation.PushAsync(new AfterSearch(klik));
     ListaNazw.SelectedItem = null;
}

Button:

async private void Button_Clicked_1(object sender, EventArgs e)
{
  await Navigation.PushAsync(new SrchPage());   
}

XAML:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="Ap.SrchPage "
         BackgroundImage="@drawable/main2_rc3.png"
         NavigationPage.HasNavigationBar="False">


<RelativeLayout>
    <ListView x:Name="ListaNazw" SeparatorColor="#3d122c" HasUnevenRows="True"
              RelativeLayout.HeightConstraint="{ConstraintExpression Property=Height,Factor=1,Type=RelativeToParent}"
              RelativeLayout.WidthConstraint="{ConstraintExpression Property=Width,Factor=1,Type=RelativeToParent}"  ItemSelected="ListaNazw_ItemSelected" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Orientation="Horizontal"  Padding="5" BackgroundColor="#90FFFFFF">
                        <StackLayout HorizontalOptions="StartAndExpand">
                            <Label Text="{Binding nazwa}" TextColor="#680345" FontSize="17"/>
                            <Label Text="{Binding opis}" TextColor="#30032a" />
                        </StackLayout>
                    </StackLayout> 
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</RelativeLayout>
</ContentPage>

Upvotes: 2

Views: 4632

Answers (2)

lowleetak
lowleetak

Reputation: 1382

In your Xaml x:class is having additional space behind.

x:Class="Ap.SrchPage "

Remove that extra space, clean build, remove all bin and obj folder. Then rebuild again.

Upvotes: 1

Gurveer Singh
Gurveer Singh

Reputation: 43

Use this and your code will work fine.

await Navigation.PushModalAsync(new SrchPage()); 

Upvotes: 1

Related Questions