-1

I am developing and android application that offers user registration and login. In the Navigation Drawer I put two TextViews for Login and Register. But, Where ever I put the OnClickListener for these TextViews it gives an error:

Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

please check this image, The TextViews are on the top of navigation drawer

enter image description here

Help. Thank You

Edited- Code

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {

       // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        child=menu.findItem(R.id.action_cart).getActionView();
        count_tv=(TextView)child.findViewById(R.id.cart_count_tv);

        g=Globals.getInstance();


        NavigationView navigationView=
        (NavigationView)findViewById(R.id.nav_view);
        View headerView=navigationView.getHeaderView(0);

        TextView username=(TextView)findViewById(R.id.usernameNavBar);
        TextView email=(TextView)findViewById(R.id.emailNavBar);

        withLogin=(FrameLayout)findViewById(R.id.login_frame_layout);
        wdoutLogin=(LinearLayout)findViewById(R.id.not_login_frame_layout);


        if(g.loginstatus==true) {
            withLogin.setVisibility(View.VISIBLE);
            username.setText("name");
            email.setText("email");
        }else {

            wdoutLogin.setVisibility(View.VISIBLE);
        }


                login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(HomeActivity.this, "Login button pressed", Toast.LENGTH_SHORT).show();
            }
        });
}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163

2 Answers2

1

Try this:

 NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

 headerView = navigationView.getHeaderView(0);

 tvLogin = (Textview) headerView.findViewById(R.id.login_textview_id);

Using headerView you have to cast your TextView. Then apply on click listner on textview.

Vidhi Dave
  • 5,614
  • 2
  • 33
  • 55
0
NavigationView navigationView = (NavigationView)findViewById(R.id.nav_view);

View header=navigationView. findViewById(R.id.header);                           
view=navigationView.inflateHeaderView(R.layout.nav_header_main);*/
login = (TextView)header.findViewById(R.id.login);
register = (TextView)header.findViewById(R.id.register);
login.setOnClickListener(this);
register.setOnClickListener(this);

you should inflate the header view and using that view you have to cast your TextView. Hope this may solve your problem.

You didnt cast the TextView or called by id. so the the Suystem cant find something with a name login

Tomin B Azhakathu
  • 2,656
  • 1
  • 19
  • 28