0

In my Robolectric test I run my fragment by:

sut = new TestedFragment();
SupportFragmentTestUtil.startFragment(sut);

In fragment's onCreateView I have android.support.v4.app.FragmentTabHost on which I try to get its tabWidget using:

inflatedView = inflater.inflate(R.layout.some_layout, container, false);
tabHost = (FragmentTabHost) inflatedView.findViewById(android.R.id.tabhost);
fragmentManager = getChildFragmentManager();
tabHost.setup(getActivity(), fragmentManager, android.R.id.tabcontent);
tabHost.getTabWidget(); //returns null

This line returns null although set up breakpoint on this line shows me that tabHost's mTabWidget has value of android.widget.TabWidget{7a4d42e6 V.E..... ......I. 0,0-0,0 #1020013 android:id/tabs} so why do I get null?

The code works on android, i doesn't work in roboelectric unit test

apex39
  • 603
  • 1
  • 6
  • 20

2 Answers2

0

Robolectric use shadow classes to hide some real code. Because not all can be done at JVM or must be adjusted.

Open the class ShadowTabHost and set a breakpoint in the getTabWidget method. Then you can see why you get null.

nenick
  • 7,340
  • 3
  • 31
  • 23
-1
Try this

 public class MainActivity extends TabActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //setContentView(R.layout.activity_main);

        TabHost mTabHost = getTabHost();

        mTabHost.addTab(mTabHost.newTabSpec("first").setIndicator("First").setContent(new Intent(this  ,FirstActivity.class )));
        mTabHost.addTab(mTabHost.newTabSpec("second").setIndicator("Second").setContent(new Intent(this , SecondActivity.class )));
        mTabHost.setCurrentTab(0);


    }
    }
user1530779
  • 409
  • 5
  • 8