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 !

  • AmaanAmaan Amaan USMember
     void listview_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
        {
            RunOnUiThread(()=>
                {
                    alert1 = new AlertDialog.Builder(this);
                    alert1.SetTitle("Network Problem");
                    alert1.SetCancelable(false);
                    alert1.SetIcon(Resource.Drawable.alertimage);
                    alert1.SetMessage("Please Check Your Internet Connection");
                    alert1.Show();
                });   
        }
    

    Application is Crashing...As i an new in Xamarin...Please help me to do the needful

  • AmaanAmaan Amaan USMember

    I had mentioned AlertDialog.Builder alert1;

  • CheesebaronCheesebaron Tomasz Cielecki DKMember, Insider, University ✭✭✭✭✭

    If it is crashing, provide the stack trace.

  • IdoTeneIdoTene Ido Tene USMember, University

    I'm getting a crash error as well, this is my stack trace:

    01-27 16:34:50.760 I/MonoDroid( 1498): UNHANDLED EXCEPTION:

    01-27 16:34:50.777 I/MonoDroid( 1498): System.NullReferenceException: Object reference not set to an instance of an object

    01-27 16:34:50.777 I/MonoDroid( 1498): at Xamarin.Forms.Platform.Android.FormsApplicationActivity.OnPrepareOptionsMenu (Android.Views.IMenu)

    01-27 16:34:50.777 I/MonoDroid( 1498): at Android.App.Activity.n_OnPrepareOptionsMenu_Landroid_view_Menu_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.App.Activity.cs:3327

    01-27 16:34:50.777 I/MonoDroid( 1498): at (wrapper dynamic-method) object.9c834ab5-1c57-4fe2-a2cd-349bdd3f3b13 (intptr,intptr,intptr)

    Please note, if I'm calling to CloseApp and then LoadApplication, then the alert is shown on the mainActivity, however if I don't call LoadApplication, then the app crashes .

    In addition, I'm using Xamarin forms, and my Activity inherits XFormsApplicationDroid class.
    This is my Activity class code:

     [Activity(Theme = "@style/Theme.MainBackground", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
        public class MainActivity : XFormsApplicationDroid
        {
            private DateTime _lastPressedTime = DateTime.UtcNow;
    
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
    
                Forms.Init(this, bundle);
    
                var isSuccess = AppStart.Init();
                if (!isSuccess)
                {
                    CloseApp();
                    // In order not to crash, unmark the next line:
                    // LoadApplication(new AndroidApp());
                }
                else
                {
                    LoadApplication(new AndroidApp());
                }
            }
    
            private void CloseApp()
            {
                var builder = new AlertDialog.Builder(this);
                builder.SetTitle("Something went wrong...");
                builder.SetMessage("Oops!Something went wrong... Pls try again later");
                builder.SetCancelable(false);
                builder.SetPositiveButton("OK", delegate { Finish(); });
                builder.Show();
            } 
    
Sign In or Register to comment.