0

I have a problem, whenever I try to add an eventhandler to a button I get a null reference exception, I am trying to create a popup window with a DialogFragment, where inside it I'm calling the view PopUpWindow which will show up on screen, but when I try to access the buttons by id and to assign them eventhandlers for example:

Button btnCopyText = dp.view.FindViewById<Button>(Resource.Id.btnCopyText);
btnCopyText.Click += BtnCopyText_Click;

then I get a null reference exception, can anyone help me, below is the necessary code.

class dialog_Popup:DialogFragment
    {
        public View view;
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreateView(inflater, container, savedInstanceState);
            view = inflater.Inflate(Resource.Layout.PopupWindow, container, false);
  
            return view;
        }
        public override void OnActivityCreated(Bundle savedInstanceState)
        {
            Dialog.Window.RequestFeature(WindowFeatures.NoTitle);
            base.OnActivityCreated(savedInstanceState);

        }

    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
         //some code
        }
        public string itemclicked;
        dialog_Popup dp;


        private void Lv_ItemLongClick(object sender, AdapterView.ItemLongClickEventArgs e)
        {
            //View popUpView = LayoutInflater.Inflate(Resource.Layout.PopupWindow,
            //null); // inflating popup layout

            Button height = FindViewById<Button>(Resource.Id.btnCopyText);
            //Then: change the width of the button
            FragmentTransaction transaction = FragmentManager.BeginTransaction();
            dp = new dialog_Popup();
            dp.Show(transaction,"Popup");

            itemclicked = lv.GetItemAtPosition(e.Position).ToString();

            Button btnCopyText = dp.view.FindViewById<Button>(Resource.Id.btnCopyText);
            btnCopyText.Click += BtnCopyText_Click;
            Button btnSaveCurrentAya = dp.view.FindViewById<Button>(Resource.Id.btnSaveCurrentAya);
            btnSaveCurrentAya.Click += BtnSaveCurrentAya_Click;
            Button btnsavingsAya = dp.view.FindViewById<Button>(Resource.Id.savingsAya);
            btnsavingsAya.Click += BtnsavingsAya_Click;*

            Button btnShareFB = dp.view.FindViewById<Button>(Resource.Id.fbShare);
            btnShareFB.Click += BtnShareFB_Click;
        }
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Muhamed Krasniqi
  • 1,134
  • 1
  • 13
  • 20

1 Answers1

0

There are several reasons why a NullReferenceException can occur with FindViewById:

  • The layout does not contain the id -> check that the correct layout and id is inflated/ referenced
  • The type like Button is incorrect

In your case, check that dp and dp.view is not null.

One thing to mention here is, that it is not the best implementation to reference the control of a fragment in you main view. A fragment is something that should be able to life on her own. So I see two ways of implementing your desired behavior:

1) The fragment gets an event and you listen to that. This means your main view will contain the logic to save something.

2) The logic moves into the fragment.

tequila slammer
  • 2,821
  • 1
  • 18
  • 25