Reputation: 2058
I've followed the tutorial https://www.mkyong.com/android/android-custom-dialog-example/
But I'm using the xamarin android and code snippets are in .net.
main layout - dialog_main and custom dialog layout - CustomDialog. The MainActivity code snippets as below.
base.OnCreate(bundle);
SetContentView(Resource.Layout.dialog_main);
Button button = FindViewById<Button>(Resource.Id.ShowDialog);
button.Click += delegate
{
Dialog dialog = new Dialog(this);
dialog.SetContentView(Resource.Layout.CustomDialog);
dialog.SetTitle("this is my custom dialog");
dialog.SetCancelable(true);
TextView textView = FindViewById<TextView>(Resource.Id.CustomDlgTextView);
textView.SetText(Resource.String.dialogtext);
Button btn = FindViewById<Button>(Resource.Id.CustomDlgButton);
btn.Click += delegate
{
dialog.Dismiss();
};
dialog.Show();
};
I'm not getting the TextView and Button elements and it throws the null exception. But while compiling the application successfully build and i'm able to see the Resource.Id have the elements.
TextView textView = FindViewById<TextView>(Resource.Id.CustomDlgTextView);
Upvotes: 0
Views: 96
Reputation: 1290
Since those view you are trying to find are belong to the dialog
, you should call FindViewById
on dialog
object. Like this:
// ...
TextView textView = dialog.FindViewById<TextView>(Resource.Id.CustomDlgTextView);
// ...
Button btn = dialog.FindViewById<Button>(Resource.Id.CustomDlgButton);
When you call FindViewById
inside an activity, it tries to find that view inside your activity and since those two view does not belong to activity itself, it can not find them.
Hope it helps.
Upvotes: 1