I want to customise the alert dialog title background and the app crashes without showing the alert dialog. (The custom view only fill the message area excluding the title panel and buttons panel. I want to customise the default title panel and buttons panel. Here I take title as an example.)
public static class MyAlertDialog
{
private static AlertDialog _alertDialog;
public static void Show(Context context)
{
var factory = LayoutInflater.From(context);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context)
.SetTitle("myTitle")
.SetView(factory.Inflate(Resource.Layout.DialogRegister, null))
.SetCancelable(true);
_alertDialog = alertDialogBuilder.Create();
var titleView = (TextView)_alertDialog.FindViewById(Android.Resource.Id.Title); //get title view from the Android resource not from my custom view
//titleView.SetBackgroundResource(Resource.Color.PrimaryColor);
titleView.SetBackgroundColor(Android.Graphics.Color.Red);
_alertDialog.Show();
}
}
I call the dialog from the main acitivity:
[Activity(Label = "My Activity", MainLauncher = true)]
public class HomeActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.home);
Button btnMyButton = FindViewById<Button>(Resource.Id.MyButton);
btnMyButton.Click += (object sender, EventArgs e) =>
{
MyAlertDialog.Show(this);
};
......
}
}
The VS throws an runtime exception and ask me to break or continue. I click on continue. Then app crashes. The log:
03-09 11:10:47.057 E/mono ( 1185): [0x4001f730:] EXCEPTION handling: Android.Util.AndroidRuntimeException: Exception of type 'Android.Util.AndroidRuntimeException' was thrown.
03-09 11:10:49.956 I/Email ( 451): ReconcilePopImapAccountsSync: start
03-09 11:10:50.276 I/Email ( 451): ReconcilePopImapAccountsSync: done
03-09 11:11:07.417 E/mono ( 1185): [0x4001f730:] EXCEPTION handling: Java.Lang.NullPointerException: Exception of type 'Java.Lang.NullPointerException' was thrown.
03-09 11:11:07.727 E/mono ( 1185): [0x4001f730:] EXCEPTION handling: Java.Lang.NullPointerException: Exception of type 'Java.Lang.NullPointerException' was thrown.
03-09 11:11:16.846 F/ ( 1185): * Assertion: should not be reached at /Users/builder/data/lanes/monodroid-lion-bigsplash/0e0e51f9/source/mono/mono/mini/debugger-agent.c:5980
03-09 11:11:16.846 I/mono ( 1185): Stacktrace:
03-09 11:11:16.846 I/mono ( 1185):
03-09 11:11:16.866 E/mono ( 1185): [0x2a118c70:] EXCEPTION handling: System.NullReferenceException: Object reference not set to an instance of an object
03-09 11:11:16.906 E/mono ( 1185):
03-09 11:11:16.906 E/mono ( 1185): Unhandled Exception:
03-09 11:11:16.906 E/mono ( 1185): System.NullReferenceException: Object reference not set to an instance of an object
03-09 11:11:16.937 I/mono ( 1185): [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
I test it in the emulator with Android 4.1.2 armeabi-v7a. (The project is set to support architectures armeabi, armeabi-v7a and x86.)
Thanks for any help. (I know I could put the title with message in a custom content view. But I also want to customise other part of it. So I just take the title as an example.)
I notice although I SetTitle("myTitle"), the titleView.text is an empty string. Was I wrong in getting the default title view by
var titleView = (TextView)_alertDialog.FindViewById(Android.Resource.Id.Title);
Again, my custom view does not contain the title view.