Application Xamarin.Forms stopped out of nowhere

Asked

Viewed 141 times

0

I was making a code to receive notification and suddenly the application does not run anymore. Compile, Deploy, but it takes time to consume the service and appears this message: O Android project stopped continue/ok. My Mainactivity:

using System;

using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

using Android.Gms.Common;
using Firebase.Messaging;
using Firebase.Iid;
using Android.Util;
using PushNotification.Plugin;
using Newtonsoft.Json.Linq;
using PushNotification.Plugin.Abstractions;
using Android.Content;
using Java.Lang;

namespace Autorizador.Droid
{
    [Activity(Label = "Autorizador", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : //global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
         global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
    {
        public static Context AppContext;
        protected override void OnCreate(Bundle bundle)
        {
            //TabLayoutResource = Resource.Layout.Tabbar;
            //ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);

            //AppContext = this.ApplicationContext;

            //CrossPushNotification.Initialize<CrossPushNotificationListener>("840100012845");

            //StartPushService();

            LoadApplication(new App());            
        }

        public static void StartPushService()
        {
            AppContext.StartService(new Intent(AppContext, typeof(PushNotificationService)));

            if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat)
            {

                PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(PushNotificationService)), 0);
                AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
                alarm.Cancel(pintent);
            }
        }

        public static void StopPushService()
        {
            AppContext.StopService(new Intent(AppContext, typeof(PushNotificationService)));
            if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat)
            {
                PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(PushNotificationService)), 0);
                AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
                alarm.Cancel(pintent);
            }
        }
    }
}

and my App.xaml.Cs

using PushNotification.Plugin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Xamarin.Forms;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;

namespace Autorizador
{
    public partial class App : Application
    {
        public static CancellationTokenSource CancellationToken { get; set; }
        private Label _label = new Label();
        public App()
        {
            InitializeComponent();

            //MainPage = new Autorizador.MainPage();
            MainPage = new NavigationPage(new Autorizador.MainPage());
        }


        protected override void OnStart()
        {
            //CrossPushNotification.Current.Unregister();
            //CrossPushNotification.Current.Register();
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }
}

I posted them using them, because maybe something shouldn’t be. What I did and did all week, comment and uncomment what is on Mainactivity, in an attempt to receive notifications.

EDIT1

I put a break on several points, but when it comes to Initializalizecomponent(), the system to. I F10 and nothing, the cursor is in the same place. Below the Mainpage class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Autorizador.Model;
using Autorizador.Service;

namespace Autorizador
{
    public partial class MainPage : ContentPage
    {
        DataService dataService;
        List<Liberacao> lib;
        int _idorcamento = 0;
        public MainPage()
        {
            InitializeComponent();
            dataService = new DataService();
            AtualizaDados();
            //Content = new ScrollView { Content = listaLibera, Orientation = ScrollOrientation.Both };
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();

            LimpaGeral();

            AtualizaDados();
        }

        private async void AtualizaDados()
        {
            lib = await dataService.GetLiberaAsync();
            listaLibera.ItemsSource = lib.OrderBy(item => item.Cliente).ToList();
        }

        private void listaLibera_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            try
            {
                var libera = e.SelectedItem as Liberacao;

                _idorcamento = libera.IdOrcamento;

                lblTipoVenda.Text = "Tipo de Venda: " + libera.TipoVenda;
                lblVencimento.Text = "Vencimento: " + libera.Vencimento;
                lblJuros.Text = "Juros: " + libera.Juros.ToString();
                lblEntrada.Text = "Entrada: " + libera.Entrada;
                lblAcrescimo.Text = "Acréscimo: " + libera.Acrescimo;
                lblDesconto.Text = "Desconto: " + libera.Desconto;
                btnItens.IsEnabled = true;
            }
            catch(Exception ex)
            {
                btnItens.IsEnabled = false;
                throw new Exception(ex.Message);
            }
        }

        private async void btnItens_Clicked(object sender, EventArgs e)
        {
            await Navigation.PushModalAsync(new MainPageItens(_idorcamento, lib));
        }
        private void LimpaGeral()
        {
            lblTipoVenda.Text = "Tipo de Venda:";
            lblVencimento.Text = "Vencimento:";
            lblJuros.Text = "Juros:";
            lblEntrada.Text = "Entrada:";
            lblAcrescimo.Text = "Acréscimo:";
            lblDesconto.Text = "Desconto:";
            btnItens.IsEnabled = false;
        }
    }
}
  • 1

    Things don’t stop at nothing, making codes without criteria causes a lot of side effects. Some problems will run days, months or years later, because the code has no consistency. It doesn’t just work, it has to be right. To know if you are right you need to understand every aspect of it. This way you will be running behind the tail.

  • @bigown, as much as I run, there’s no way I won’t take your comments. I know that, the point is, we think we’re doing the right thing. Everything was ok, I went out to lunch and when I came back to rotate, it didn’t work. But you’re right. If you do it right, it comes out right, rs.

1 answer

0

In my MainPage.xaml there was this:

<DataTemplate>
<ViewCell.View>

When I should have just this

<DataTemplate>
<ViewCell>

Honestly, I don’t know how it got like this. I tried before all this, to change the background color of a cell when it is selected in the listview, but unsuccessfully, then I removed everything. It may be that when inserting a stacklayou(was what I did), changed the ViewCell and I didn’t realize, and I was running behind the ass as Maniero stated assertively.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.