0

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"));
    }
}
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95

4 Answers4

2

Change 1 : instead of import android.support.v4.app.ActionBar use import android.support.v7.app.ActionBar in your activity

Change 2 : change this actionBar = getActionBar(); to actionbar = getSupportActionBar();

Update:

You have to use PagerSlidingTabStrip because setNavigationMode is now deprecated.

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
1

It's been long time since ActionBar got deprecated.

Try to use Toolbar and SlidingTabs.

Please refer to this answer for same question. It will provide you enough guide for your query.

Android, Tabs without Actionbar

You can easily implement this and it will be easy for you to handle onClick() events for tabs.

Community
  • 1
  • 1
Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
0

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

0

You should Try to change your parent style

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
Learning Always
  • 1,563
  • 4
  • 29
  • 49