Menu does not call Activity

1

My menu does not call Activity, I already did some tests and it does not work, I do not know what else to do.

Can anyone help me?

package com.chl.infotrans.infotrans;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

public class LineActivity extends ActionBarActivity {

    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_line);

        mToolbar = (Toolbar) findViewById(R.id.tb_main);
        mToolbar.setTitle("Linhas");
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                Intent it = null;

                switch(menuItem.getItemId()){
                    case R.id.action_news:
                        Intent intent = new Intent(getBaseContext(), MainActivity.class);
                        startActivity(intent);
                        break;
                    case R.id.action_map:
                        Intent intent1 = new Intent(getBaseContext(), MapActivity.class);
                        startActivity(intent1);
                        break;
                    case R.id.action_menu:
                        Intent intent2 = new Intent(getBaseContext(), MenuActivity.class);
                        startActivity(intent2);
                        break;
                }

                startActivity(it);
                return true;
            }
        });

        mToolbar.inflateMenu(R.menu.menu_social);

        if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setNavigationBarColor(getResources().getColor(R.color.blue04));
        }
    }
}

I have already used all the alternatives and nothing. This is the error:

  

java.lang.RuntimeException: Unable to start activity ComponentInfo {com.chl.infotrans.infotrans / com.chl.infotrans.infotrans.LineActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support .v7.widget.Toolbar.setTitle (java.lang.CharSequence) 'on a null object reference

and

  

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar.setTitle (java.lang.CharSequence)' on a null object reference

    
asked by anonymous 20.05.2015 / 00:47

1 answer

2

You have to add setSupportActionBar(mtoolbar) to your toolbar , eg:

mToolbar = (Toolbar) findViewById(R.id.tb_main);
setSupportActionBar(mtoolbar);

mToolbar.setTitle("Linhas");
    
21.05.2015 / 05:14