Menu does not call Activity

Asked

Viewed 220 times

1

My menu doesn’t call Activity, I’ve done some tests and it doesn’t work, I don’t know what else to do.

Can someone 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’ve used all the alternatives and nothing. This is the mistake:

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

  • 1

    Welcome, it is important that the question is as detailed as possible. Feel free to explore.http://answall.com/tour

  • Put the complete exception so we can see which part of your java code the error occurs.

1 answer

2

You have to add setSupportActionBar(mtoolbar) for your Toolbar, ex:

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

mToolbar.setTitle("Linhas");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.