1

What does Attempt to invoke virtual method 'void android.app.ActionBar.setNavigationMode(int)' on a null object reference meant? I tried many solution but no one can solve my problem.

Tab.java

package com.example.project.project;
     import android.os.Bundle;
    import android.app.ActionBar;
    import android.app.FragmentTransaction;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.view.ViewPager;


public class Tab extends FragmentActivity {
         ViewPager Tab;
         TabPagerAdapter TabAdapter;
         ActionBar actionBar;

         @Override
         protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.tab1);

             TabAdapter = new TabPagerAdapter(getSupportFragmentManager());


             Tab = (ViewPager)findViewById(R.id.pager);

             Tab.setOnPageChangeListener(
                     new ViewPager.SimpleOnPageChangeListener() {
                         @Override
                         public void onPageSelected(int position) {

                             actionBar = getActionBar();
                             actionBar.setSelectedNavigationItem(position);                    }
                     });

             Tab.setAdapter(TabAdapter);

             actionBar = getActionBar();

             //Enable Tabs on Action Bar
             actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
             ActionBar.TabListener tabListener = new ActionBar.TabListener(){

                 public void onTabReselected(android.app.ActionBar.Tab tab,
                                             FragmentTransaction ft) {
                     // TODO Auto-generated method stub
                     //Toast.makeText(getApplicationContext(), "Tab selected", 2000).show();

               }

                 @Override
                 public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

                     Tab.setCurrentItem(tab.getPosition());
                 }

                 @Override
                 public void onTabUnselected(android.app.ActionBar.Tab tab,
                                             FragmentTransaction ft) {
                     // TODO Auto-generated method stub

                 }};
             //Add New Tabs
             actionBar.addTab(actionBar.newTab().setText("Information").setTabListener(tabListener));
             actionBar.addTab(actionBar.newTab().setText("Work Force").setTabListener(tabListener));
             actionBar.addTab(actionBar.newTab().setText("Work Details").setTabListener(tabListener));

         }




     }

TabPagerAdapter.java

package com.example.project.project;

        import android.support.v4.app.Fragment;
        import android.support.v4.app.FragmentManager;
        import android.support.v4.app.FragmentStatePagerAdapter;

public class TabPagerAdapter extends FragmentStatePagerAdapter {
    public TabPagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

   @Override
    public Fragment getItem(int i) {

        switch (i) {
            case 0:

                return new Information();

            case 1:

                return new WorkDetailsTable();
        }
        return null ;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 3; //No of Tabs you can give your number of tabs
    }


}

Tab1.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.project.project" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
        <activity
            android:name=".Tab">

        </activity>
        <activity android:name=".WorkDetailsTable" android:screenOrientation="landscape"  />
        <activity android:name="Information"/>



    </application>

</manifest>

Error

10-04 11:11:07.492    2146-2146/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.project.project, PID: 2146
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.project.project/com.example.project.project.Tab}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setNavigationMode(int)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)

And in my Mainfest.xml, the compiler said

<activity android:name=".WorkDetailsTable" android:screenOrientation="landscape"  />
        <activity android:name="Information"/>

is not assignable to android.app....

Hoo
  • 1,806
  • 7
  • 33
  • 66

3 Answers3

2

Try replacing

 actionBar = getActionBar();

with

 actionBar = ((AppCompatActivity) activity).getSupportActionBar();

where

Activity activity = (Activity) this;
varunkr
  • 5,364
  • 11
  • 50
  • 99
  • variable activity is accessed from within inner calss,need to be declared final – Hoo Oct 04 '15 at 12:09
  • Please decare activity at the top with viewPager, ActionBar etc, else try making it final Activity actvity = (Activity) this – varunkr Oct 04 '15 at 12:13
  • After trying your method and import android.support.v7.app.ActionBar; , I get this ActionBar.TabListener tabListener = new ActionBar.TabListener(){ must either be declared abstract or implement abstract method.. – Hoo Oct 04 '15 at 12:15
  • then please implement the abstract method – varunkr Oct 04 '15 at 12:17
  • By the way you are using a deprecated way of implementing tabs which might cause problems in future. This is the correct and recommended way. http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/ – varunkr Oct 04 '15 at 12:23
  • Thanks....can you help me figuring out my problem in AndroidMainfest.xml? It said I not assignable to android.app.Activity ....Did I miss out something? – Hoo Oct 04 '15 at 12:28
  • Can you please quote the exact error? In any case I think this will definitely help you http://stackoverflow.com/questions/30486389/activity-is-not-assigned-to-android-app-activity-manifest-xml :-) – varunkr Oct 04 '15 at 12:34
  • It totally frustrating! I think I facing the same problem like him. Sadly , some comment had moved to chat discussion and I still stuck. – Hoo Oct 04 '15 at 13:16
  • There is another error message at the ActionBar.TabListener. The error message as Class anonymous class derived from TabListener must either be declared abstract or implement abstract method onTabSelected and the Override as there are some red line highlighted underneath – Hoo Oct 04 '15 at 13:24
  • just implement the method as it is asking, and it would be fine. – varunkr Oct 04 '15 at 13:30
  • just paste the 3 tab methods as shown by Michael in the other answer and it should work – varunkr Oct 04 '15 at 13:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91298/discussion-between-hoo-and-varunkr). – Hoo Oct 04 '15 at 13:39
  • Thanks for your help...but I only can accept one answer. Between, you both are awesome :D – Hoo Oct 04 '15 at 14:18
0

Try to replace this lines:

actionBar = getActionBar();

With:

actionBar = ((AppCompatActivity) mActivity).getSupportActionBar();

And add this on top of onCreate()

Activity mActivity = (Activity) this;

And start activity with this:

 <activity
        android:name=".YourActivityClassName"
        android:label="YourActivity"
        android:screenOrientation="landscape">
        <intent-filter>
            <action android:name="package.name.YOURACTIVITYCLASSNAME" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
 </activity>

This is listener:

ActionBar.TabListener tabListener = new ActionBar.TabListener() {

        @Override
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

        }

        @Override
        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

        }

        @Override
        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
            //Toast.makeText(getApplicationContext(), "Tab selected", 2000).show();

        }

    }

Refer to this: FragmentActivity vs Activity

Also remember to use Activity if you are using android.app.Fragment; use FragmentActivity if you are using android.support.v4.app.Fragment. Never attach a android.support.v4.app.Fragment to an android.app.Activity, as this will cause an exception to be thrown.

Community
  • 1
  • 1
Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54
  • I'm sorry I did not see who was FragmentActivity give me a moment and I work out – Michele Lacorte Oct 04 '15 at 11:54
  • After trying your method and import android.support.v7.app.ActionBar; , I get this ActionBar.TabListener tabListener = new ActionBar.TabListener(){ must either be declared abstract or implement abstract method... – Hoo Oct 04 '15 at 12:14
  • I add the new listener :D enjoy – Michele Lacorte Oct 04 '15 at 12:19
  • Thanks....can you help me figuring out my problem in AndroidMainfest.xml? It said I not assignable to android.app.Activity – Hoo Oct 04 '15 at 12:26
  • ok, I have removed these from mainfest.xml and use your code. But after I changed it there is another error message at the ActionBar.TabListener. The error message as Class anonymous class derived from TabListener must either be declared abstract or implement abstract method onTabSelected and the Override as there are some red line highlighted underneath – Hoo Oct 04 '15 at 13:06
  • Have you use my code of TabListener? Because this error show when onTabSelected was not implemented... – Michele Lacorte Oct 04 '15 at 13:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91299/discussion-between-hoo-and-michele-lacorte). – Hoo Oct 04 '15 at 13:41
0
Actionbar has to be replaced with toolbar now, all you need to do is include a toolbar in your xml files. I used the appcompact library toolbar, for this you may have to add support library in your gradle file
    compile 'com.android.support:support-v4:22.0.0'
    compile 'com.android.support:appcompat-v7:22.0.0'

Add follwing toolbar to xml file 
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:theme="@style/Base.ThemeOverlay.AppCompat.Dark">

</android.support.v7.widget.Toolbar>


Don't forget to inherit your activity from Appcompactactivity, you may use your own toolbar, finally access the toolbaar in activity
  Toolbar toolbar = (Toolbar) this.findViewById(R.id.toolbar);
   setSupportActionBar(toolbar);
Ashish Rawat
  • 5,541
  • 1
  • 20
  • 17