Search code examples
androidandroid-layoutandroid-actionbarandroid-actionbar-compat

ActionBar didn't work?


My application crashes and I get

Error :

setNavigationMode(int)' on a null object reference

Code :

package com.example.muhammad_adel.tabs;


import android.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    ActionBar actionBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.addTab(actionBar.newTab().setText("one"));
        actionBar.addTab(actionBar.newTab().setText("two"));
        actionBar.addTab(actionBar.newTab().setText("three"));
    }
}

Solution

  • I find the solution thanks for your help

    First instead of import android.app.ActionBar => import android.support.v7.app.ActionBar

    Second changing this actionBar = getActionBar(); to actionbar = getSupportActionBar();

    Third you must implement TabListener