-1

I have a problem by building an OnClick Event in xamarin. When I switch from my Mainactivity to my loginActivity it says

"Unhandled Exception: Object reference not set to an instance of an object."

How can I fix it? I have many "solutions" tested, but no one have worked for me.

[Activity(Label = "Anmelden")]

public class Login : AppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.login);
        drawer();
        Button btnlogin = FindViewById<Button>(Resource.Id.login);
        btnlogin.Click += (object sender, EventArgs e) =>
        {
            btnlogin.Text = "Hello World!";
        };
    }        
    void drawer()
    {
        DrawerLayout drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
        // Init toolbar
        var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
        SetSupportActionBar(toolbar);
        // Attach item selected handler to navigation view
        var navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
        navigationView.NavigationItemSelected += NavigationView_NavigationItemSelected;
        navigationView.SetCheckedItem(Resource.Id.login);
        // Create ActionBarDrawerToggle button and add it to the toolbar
        var drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, Resource.String.open_drawer, Resource.String.close_drawer);
        drawerLayout.SetDrawerListener(drawerToggle);
        drawerToggle.SyncState();
        // Create your application here
        void NavigationView_NavigationItemSelected(object sender, NavigationView.NavigationItemSelectedEventArgs e)
        {
            switch (e.MenuItem.ItemId)
            {
                case (Resource.Id.nav_home):
                    StartActivity(typeof(MainActivity));                       
                    break;
                case (Resource.Id.login):                        
                    //StartActivity(typeof(Login));                     
                    break;
                case (Resource.Id.nav_friends):
                    break;
                case (Resource.Id.nav_discussion):
                    break;
            }
            // Close drawer
            drawerLayout.CloseDrawers();
        }
    }       
}}

Sorry for my bad English I come from Germany.

Anil
  • 1,557
  • 11
  • 22
dev
  • 1
  • 1

1 Answers1

0

Could you confirm which line trigger the NRE? Because I'll have an hard time helping you as NRE firing upon activity change can happen for several reasons.

A couple of things which bugged me :

  • You can not use FindViewById in OnCreate (and any method inside it), as the MainActivity view is not created yet. You need to deal with UI linking in OnCreateView, as stated in : NRE exception FindViewById - StackOverflow. I'm kinda convinced this is your NRE.

  • As you're using more than one activity, I suggest you should unsubscribe to any event (including OnClick) before leaving activity. I don't know if it's good practice, but I ALWAYS subscribe to events in OnResume (both activity and fragment), and ALWAYS unsubscribe in OnPause, unless I'm on a mono activity, locked rotation screen application.

Kinxil
  • 286
  • 2
  • 11
  • What does NRE mean? – dev Aug 24 '17 at 11:48
  • Null Reference Exception. Basically the one triggered here and giving you the message "Unhandled Exception: Object reference not set to an instance of an object.". – Kinxil Aug 24 '17 at 11:51
  • The NRE Occurs in line 11 at: btnlogin.Click += (object sender, EventArgs e) => { btnlogin.Text = "Hello World!"; }; – dev Aug 24 '17 at 12:00
  • No surprise then, this is because you tried to retrieve your button from a layout which have not been created/inflated yet : Button btnlogin = FindViewById – Kinxil Aug 24 '17 at 12:02
  • I can't use Activity.OnCreateView. What should I do then? – dev Aug 24 '17 at 12:33
  • Oh uh, sorry I don't really know how to help you out then. The only thing which comes to my mind would be to schedule all your UI operations on a new thread (through RunOnUiThread for each UI change !) after a "safety" sleep time integrated with arbitrary value in your thread to make sure Activity had enough time to create view, but that seems really, really dirty to me. Another, maybe better method would be to have a one time initialisation in OnResume (for example, by checking a bool, if false, proceed init and setting it to true, and setting it back to false when leaving this activity). – Kinxil Aug 24 '17 at 12:42