2

On android device 4.0.1 I am trying to build application with ActionBar but getting the NullPointException. I have tried the following solutions:

  1. Adding Theme Theme.Holo.Light to the application theme.
  2. In OnCreate of Activity, setting ActionBar feature before setContentView as requestWindowFeature(Window.FEATURE_ACTION_BAR).
  3. Used SherlockActivity and called getSupportActionBar()

But no luck. In all methods, I am getting null ActionBar. Can anyone please point me out what is the issue. I am pasting Activity and AndroidManifest.xml here.

AndroidManifest.xml

<code>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tzoomers.birthdaysdiary"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo.Light">
        <activity
            android:name="com.tzoomers.birthdaysdiary.BirthdaysDiary"
            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=".ContentActivity">
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name=".SyncActivity">
        </activity>
    </application>

</manifest>

</code>

SyncActivity.java

<code>    
public class SyncActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

        setContentView(R.layout.layout_sync_activity);

        ActionBar actionBar = getActionBar();
        if(actionBar != null)
        {
            getActionBar().setDisplayHomeAsUpEnabled(false);
        }
        else
        {
            Toast.makeText(this, "Action bar is null", Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        return super.onOptionsItemSelected(item);
    }
}

Please help what can be exact issue instead of pointing urls. I have tried all the solutions. If I am missing something in XML or JAVA files please point that.

Thanks in advance.

4
  • The application theme should be something like @style/Theme.Sherlock and the activity needs to extends SherlockActivity
    – An-droid
    May 16, 2014 at 14:05
  • I have tried that. extends activity to SherlockActivity and added theme to @style/Theme.Sherlock but still getting 'NullPointException' on getSupportActionBar() May 16, 2014 at 14:17
  • Also you don't need "getWindow().requestFeature(Window.FEATURE_ACTION_BAR);"
    – An-droid
    May 16, 2014 at 14:19
  • Yes i know there is no need to use getWindow().requestFeature(Window.FEATURE_ACTION_BAR); I have just given steps that i have followed. Used themes and requested feature. I have printed value returned from getWindow().requestFeature(Window.FEATURE_ACTION_BAR); its giving false. Is this can be the issue that i am not allowed to request these features? May 16, 2014 at 14:22

1 Answer 1

1

I JUST solved this problem myself, with the new updates its a bit tricky and some of the old fixes don't work as well anymore. Try this:

  1. set your MainActivity Java code to extend ActionBarActivity

  2. use getSupportAcionBar(); call to retrieve your action bar

  3. Be sure your (custom) TabListener extends android.support.v7.app.ActionBar.TabListener

  4. you can use FragmentManager to add and remove tab Fragments when tabs are selected and unselected.

Here's some of my code snippets to help illustrate, hope it works for you too :)

public class MainActivity extends ActionBarActivity 
{

@Override
protected void onCreate(Bundle savedInstanceState) //Overrode Default Constructor
{
    super.onCreate(savedInstanceState);
    android.support.v7.app.ActionBar tabsActionBar = getSupportActionBar();
   /***following changes ActionBar to a Tabbed ActionBar***/
    tabsActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);`  

    android.support.v7.app.ActionBar.Tab tabArray = tabsActionBar.newTab();to later contain 'inflated' digitalClock (tabbed) UI
    tabArray.setText(R.string.tab_one);
    /***following sets clockTabListener private class as Listener for
     * this object***/
    tabArray.setTabListener( new clockTabListener( this, digitalClockFragment.class.getName() ) );//actual call to digitalClockFragment
    tabsActionBar.addTab(tabArray);//adds TabArray to Action Bar Tab(s)
    /******Second call for setting New Tab to AnalogClockFragment******/
    tabArray = tabsActionBar.newTab();
    tabArray.setText(R.string.tab_two);
    tabArray.setTabListener( new clockTabListener( this, analogClockFragment.class.getName() ) );//actual call to analogClockFragment
    tabsActionBar.addTab(tabArray);


private class clockTabListener implements android.support.v7.app.ActionBar.TabListener
{

    private final Activity currentActivity;
    private final String currentFragment;
    private Fragment launchFragment;
    private android.app.FragmentManager frgManager;
    public clockTabListener(Activity activityName, String fragmentName)
    {
    currentActivity = activityName;
    currentFragment = fragmentName;
    frgManager = getFragmentManager();
    }

    /******************************************************************/
    /******************************************************************/
    /******************************************************************/
    @Override
    public void onTabSelected(android.support.v7.app.ActionBar.Tab arg0,
            android.support.v4.app.FragmentTransaction arg1) 
    {
        launchFragment = Fragment.instantiate(currentActivity, currentFragment);
        frgManager.beginTransaction().replace(android.R.id.content, launchFragment).commit();
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.