Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9127389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:16:17+00:00 2026-06-17T07:16:17+00:00

I wrote Android JUnit test for Activity that instantiates fragments (actually tabs). During the

  • 0

I wrote Android JUnit test for Activity that instantiates fragments (actually tabs). During the test, when I try to do anything with these tabs, they crash because getActivity() method in them returns null. The actual application (not a test) never shows this behavior and fragment getActivity() always returns the right parent activity there. My test case looks like:

public class SetupPanelTest extends ActivityUnitTestCase<MyAct> {

    FSetup s;   

    public SetupPanelTest() {
    super(MyAct.class);
    }

    protected void setUp() throws Exception {
        super.setUp();
        startActivity(new Intent(), null, null);
        final MyAct act = getActivity();

        AllTabs tabs = act.getTabs();
        String tabname = act.getResources().getString(R.string.configuration);

        // This method instantiates the activity as said below
        s = (FSetup) tabs.showTab(tabname);
        FragmentManager m = act.getFragmentManager();
        // m.beginTransaction().attach(s).commit(); 
        //     ... and even this does not help when commented out

        assertTrue(s instanceof FSetup);  // Ok     
        assertEquals(act, s.getActivity()); // Failure
    }

    public void testOnPause() {
        // this crashes because s.getActivity == null;
        s.onPause();
        }
 }

The AllTabs creates a fragment, then required, in this way:

 FragmentManager manager = getFragmentManager();
 Fragment fragment = manager.findFragmentByTag(tabname);
 if (fragment == null || fragment.getActivity() == null) {
      Log.v(TAG, "Instantiating ");
      fragment = new MyFragment();
      manager.beginTransaction().replace(R.id.setup_tab, fragment, tabname).commit();
 ....

Here, all fragments are initially placeholders that are later replaced by the actual fragments:

<FrameLayout
  android:id="@+id/setup_tab"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" />    

The logcat shows that the new fragment has been instantiated. In the same layout, there is also the previously mentioned AllTabs fragment that seems not having this problem (where and how it gets FragmentManager otherwise):

<TabWidget
     android:id="@android:id/alltabs"
...

Most impressively, when I call attach directly on the fragment manager obtained on the right activity, this still has no effect. I tried to put five seconds delay (I have read that transaction may be delayed), I tried to call the rest of the test through runOnUiThread – nothing helps.

The question is that is need to do so to attach my fragments to the activity also during the test. I have fragment and I have activity, I cannot attach one to another.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:16:18+00:00Added an answer on June 17, 2026 at 7:16 am

    Even if you call .commit() on transaction, it is still not done, fragments are attached only lazily.

        FragmentManager m = activity.getFragmentManager();
        m.executePendingTransactions();
    

    This finally attaches all fragments to the activity. Seems redundant when running the application itself but required in JUnit test case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote android code to send data that taken from textbox from one activity
I wrote an android program that: has a main activity for UI, and it
I am writing junit test cases for an android project. I wrote junit for
I wrote an android app for someone, but they want the source code because
I am developing a simple android application using eclipse. I wrote a JUnit TestCase
I wrote an Android application. These are the permissions I requested: INTERNET ACCESS-COARSE-LOCATION ACCESS-FINE-LOCATION
I wrote a simple Android program with 2 tabs. Tab 1 has a button
I wrote an android application that captures a live preview from the camera. It
I wrote a simple Android program with 2 tabs. Tab 1 has a button
I wrote a simple Android program with 2 tabs. Tab 1 has a button

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.