AlertDialog doesn't show

DahevosDahevos Dahevos DahevosFRMember

Hello,

I get product from the database (async method). For that I use eventHandler method. If it failed, I want to display a message. For testing, I want to always display the AlertDialog that's why you can see a if(false)...

Here is my code :

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

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

namespace Catalog
{
    [Activity(MainLauncher = true, Label = "Catalog", NoHistory = true, Icon = "@drawable/icon")]
    public class SplashActivity : Activity
    {


        protected override void OnCreate(Bundle bundle)
        {

            base.OnCreate(bundle);


            // Set our view from the "splashscreen" layout resource
            SetContentView(Resource.Layout.Splashscreen);        

            Suscribe();
            ProductManager.Instance.GetAllProductsAsync();

        }


        private void Suscribe()
        {
            ProductManager.Instance.RequestEnded += LoadingFinished;
        }

        private void LoadingFinished(Boolean result)
        {
            if (false)
            {
                StartActivity(typeof(MainActivity));
            }
            else
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.SetTitle("Error");
                builder.SetMessage("Can't connect to the database.");
                builder.SetCancelable(false);
                builder.SetPositiveButton("OK", delegate { Finish(); });
                builder.Show();
            }
        }

    }
}

The code is executed (if I put a breakpoint, I reached it) but no alertDialog appeared. If I put the same code into the OnCreate method, it's works.

Why ? How can I fix that ? Thanks.

Posts

  • ChWoChWo Ch Wo DEMember

    Try to show your dialog like it is done in this sample: Thanks @Cheesebaron!

    https://github.com/Cheesebaron/MonoDroid.DatePickerSample/blob/master/MonoDroid.DatePickerSample/Activity1.cs

    So define a unique integer dialogId. Show your dialog with ShowDialog(MY_UNIQUE_DIALOG_ID);

    And override the OnCreateDialog method.

    protected override Dialog OnCreateDialog(int id)
            {
                switch (id)
                {
                    case MY_UNIQUE_DIALOG_ID:
                        return newAlertDialog(this, etc, etc, etc);
                }
                return null;
            }
  • DahevosDahevos Dahevos Dahevos FRMember
    edited February 2013

    Bug:
    "Can't create handler inside thread that has not called Looper.prepare()"

    I think is because I use event, but don't know how to handle with that.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    
    namespace Catalog
    {
        [Activity(MainLauncher = true, Label = "Catalog", NoHistory = true, Icon = "@drawable/icon")]
        public class SplashActivity : Activity
        {
    
    
            protected override void OnCreate(Bundle bundle)
            {
    
                base.OnCreate(bundle);
    
    
                // Set our view from the "splashscreen" layout resource
                SetContentView(Resource.Layout.Splashscreen);        
    
                Suscribe();
                ProductManager.Instance.GetAllProductsAsync();
    
            }
    
    
            private void Suscribe()
            {
                ProductManager.Instance.RequestEnded += LoadingFinished;
            }
    
            protected override Dialog OnCreateDialog(int id)
            {
    
                AlertDialog.Builder builder;
                Dialog dialog = null;
                switch (id)
                {
                    case 0:
                         builder = new AlertDialog.Builder(this);
                         builder.SetTitle("Error");
                         builder.SetMessage("Can't connect to the database.");
                         builder.SetCancelable(false);
                         builder.SetPositiveButton("OK", delegate { Finish(); });
                         dialog = builder.Create();
                         break;
                }
                return dialog;
            }
    
    
            private void LoadingFinished(Boolean result)
            {
                if (false)
                {
                    StartActivity(typeof(MainActivity));
                }
                else
                {
                    ShowDialog(0);
    
                }
            }
    
        }
    }
    
  • TeHaTeHa Tobias Heller DEMember ✭✭

    Call the

    ShowDialog(0);

    in the RunOnUIThread-Method:

    RunOnUIThread(()=>ShowDialog(0));

  • DahevosDahevos Dahevos Dahevos FRMember

    Thanks ! I try with that and it works too :

     private void LoadingFinished(Boolean result)
            {
                if (!result)
                {
                    StartActivity(typeof(MainActivity));
                }
                else
                {
                    RunOnUiThread(() =>
                 {
                     AlertDialog.Builder builder;
                     builder = new AlertDialog.Builder(this);
                     builder.SetTitle("Error");
                     builder.SetMessage("Can't connect to the database.");
                     builder.SetCancelable(false);
                     builder.SetPositiveButton("OK", delegate { Finish(); });
                     builder.Show();
                 }
                 );
                }
            }
    

    So the problem was RunOnUiThread() ! Thanks a lot !

Sign In or Register to comment.