Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't manage to use tabs with Sherlock
    primarykey
    data
    text
    <p>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.</p> <p>Here's part of the error </p> <pre><code>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) </code></pre> <p>Here is one of my fragment :<br> <strong>HomeFragment</strong></p> <pre><code>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); } } </code></pre> <p>Here is my tabListener :<br> <strong>MyTabListener</strong></p> <pre><code>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); } </code></pre> <p>}</p> <p>And my main activity :<br> <strong>SherlockTestActivity</strong></p> <pre><code>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); } </code></pre> <p>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 <code>ft.replace(R.id.fragment_container, fragment);</code> (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.</p> <p>So if anyone can manage to help me around here ! Thank you !</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload