ActionBarSherlock Returning Null Value in Android App

1.3k Views Asked by At

Here's my issue:

I've been trying to get an ActionBar in my Android 2.3 App. I decided to use ActionBarSherlock. I created a new project by importing the ZIP and then making it a library.

I added it as a library to my current project.

Then it wouldn't work unless I removed the compatibility library (Conversion to Dalvik format failed with error 1)

So I removed it and no more errors when compiling. But when I try to use it, it always returns null. I'm not sure if I'm missing something small or what but this has been driving me nuts for a while now. Any advice would be greatly appreciated. Here are some code snippets as well:

Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.StuMan"
  android:versionCode="1"
  android:versionName="1.0"
  android:theme="@style/Theme.Sherlock">
<uses-sdk android:minSdkVersion="4"
          android:targetSdkVersion="13" />

And then the call which I make in my Activity:

import java.util.ArrayList;
import com.StuMan.R;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.Window;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;

public class WelcomeGrid extends FragmentActivity{

private ArrayList<String> parts = new ArrayList<String>();
final String [] items=new String[]{"Classes", "Calendar", "To-Do List"};

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome_grid);
    requestWindowFeature(Window.FEATURE_ACTION_BAR_ITEM_TEXT);

    Toast.makeText(getApplicationContext(), String.valueOf(getSupportActionBar().isShowing()), Toast.LENGTH_LONG);

    GridView gv = (GridView)findViewById(R.id.gv_welcome);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);

    gv.setAdapter(adapter);

}

}

1

There are 1 best solutions below

0
scottyab On

You don't need to include the support package as ActionBarSherlock contains the support package (and enhances it). If you check the ActionBarSherlock source you'll be about to see the android.support.v4.* classes.

I was getting getSupportActionBar() retuning null till I went back to reread the usage instructions in particular about Parent Themes.

In order for the action bar to function on pre-3.0 devices your activity must use Theme.Sherlock or Theme.Sherlock.Light, or your custom theme must use one of the aforementioned two as its parent.

Include in the application element

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
        android:theme="@style/Theme.Sherlock">