MagicMicky
MagicMicky

Reputation: 3889

Can't manage to use tabs with Sherlock

Ok, so I'm trying to use Sherlock to display multiple tabs, each for one fragment. I've got only 4classes : one for my main activity, two for my fragments, and one for the TabListener. Everything should be ok (I've got quite the same program without Sherlcock, working on 4.0 devices), so I can't understand why i get that NullPointerException.

Here's part of the error

05-18 17:46:57.197: E/AndroidRuntime(9312): FATAL EXCEPTION: main
05-18 17:46:57.197: E/AndroidRuntime(9312): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.micky.testing/com.micky.testing.SherlockTestActivity}: java.lang.NullPointerException
...
05-18 17:46:57.197: E/AndroidRuntime(9312): Caused by: java.lang.NullPointerException
05-18 17:46:57.197: E/AndroidRuntime(9312):     at com.micky.testing.MyTabListener.onTabSelected(MyTabListener.java:21)
...
05-18 17:46:57.197: E/AndroidRuntime(9312):     at com.micky.testing.SherlockTestActivity.onCreate(SherlockTestActivity.java:39)

Here is one of my fragment :
HomeFragment

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.app.SherlockFragment;

public class HomeFragment extends SherlockFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.homefragment, container, false);
    }
}

Here is my tabListener :
MyTabListener

public class MyTabListener implements TabListener {

public SherlockFragment fragment;

MyTabListener(SherlockFragment fr) {
    Log.d("MYTAG", "Creating a fragmentListener w/ " + fr);
    this.fragment = fr;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
    Log.d("TAG", "" + fragment);
    ft.replace(R.id.fragment_container, fragment);
}

}

And my main activity :
SherlockTestActivity

public class SherlockTestActivity extends SherlockActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //We take the support actionbar
    ActionBar ab = getSupportActionBar();
    //We set to navigationmode with tabs
    ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    //we create the tabs
    ActionBar.Tab homeTab = ab.newTab().setText("Home");
    ActionBar.Tab tagsTab = ab.newTab().setText("Tags");

    //We create the fragments
    SherlockFragment homeFragment = new HomeFragment();
    SherlockFragment tagsFragment = new TagFragments();

    //And we set the tabsListener;
    homeTab.setTabListener(new MyTabListener(homeFragment));
    tagsTab.setTabListener(new MyTabListener(tagsFragment));

    Log.d("","" + homeTab);
    ab.addTab(homeTab);
    ab.addTab(tagsTab);
}

Ok, so the error seems to be thrown when I add the tab to my actionbar. And when I don't add the TabListener to the tab, there's no error. The code ft.replace(R.id.fragment_container, fragment); (MyTabListener) seems to be the problem, but i can't manage to understand why. fragment isn't null (initialized when instanciating a new tabListener), and there's no reason the fragment_container is wrong.

So if anyone can manage to help me around here ! Thank you !

Upvotes: 3

Views: 3144

Answers (1)

dymmeh
dymmeh

Reputation: 22306

You should be extending SherlockFragmentActivity instead of SherlockActivity. Use a fragment activity when you are managing fragments.

Upvotes: 8

Related Questions