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 8965791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:53:11+00:00 2026-06-15T16:53:11+00:00

I have done fragments with tabs before by using a tablistener and having a

  • 0

I have done fragments with tabs before by using a tablistener and having a class for each fragment, now I’ve figured out that Eclipse lets you set up either tabs or sections with fragments, so I used this instead. But now I have a problem. I created a fragment class inside my mainactivity class. But when I try to change the data in a textview I get a nullpointer.

Here is the code

public class MainActivity extends FragmentActivity {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for    each of the
 * sections. We use a {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will
 * keep every loaded fragment in memory. If this becomes too memory intensive, it may be best
 * to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Create the adapter that will return a fragment for each of the three primary sections
    // of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());


    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setCurrentItem(2);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@SuppressWarnings("deprecation")
@Override
public boolean onOptionsItemSelected(MenuItem item) {


    return true;
}


/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
 * sections of the app.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new MainFragment();
        Bundle args = new Bundle();
        args.putInt(MainFragment.ARG_SECTION_NUMBER, i);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0: return getString(R.string.title_section1).toUpperCase();
            case 1: return getString(R.string.title_section2).toUpperCase();
            case 2: return getString(R.string.title_section3).toUpperCase();
            case 3: return getString(R.string.title_section4).toUpperCase();
        }
        return null;
    }
}


public static class MainFragment extends Fragment {

    public static final String ARG_SECTION_NUMBER = "section_number";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        int position = args.getInt(ARG_SECTION_NUMBER);
        int tabLayout = 1;

        switch(position) {
        case 0:
            tabLayout = R.layout.tab1;
            break;
        case 1:
            tabLayout = R.layout.tab2;
            break;
        case 2:
            tabLayout = R.layout.tab3;
            break;
        case 3:
            tabLayout = R.layout.tab4;
            break;

        }

        View view = inflater.inflate(tabLayout, container, false);

        TextView tv = (TextView) getActivity().findViewById(R.id.stats_squats);
        tv.setText("DEPRSTgjhndkpgfhn");




        return view;
    }

}

I have tried different ways to do this:

        TextView tv = (TextView) getActivity().findViewById(R.id.stats_squats);
        tv.setText("DEPRSTgjhndkpgfhn");

        TextView tv = (TextView) getView().findViewById(R.id.stats_squats);
        tv.setText("DEPRSTgjhndkpgfhn");

        TextView tv = (TextView) view.findViewById(R.id.stats_squats);
        tv.setText("DEPRSTgjhndkpgfhn");

Same error each time.

Here is the logcat log:

12-10 17:37:56.898: D/OpenGLRenderer(13574): Enabling debug mode 0
12-10 17:37:58.970: D/AndroidRuntime(13574): Shutting down VM
12-10 17:37:58.970: W/dalvikvm(13574): threadid=1: thread exiting with uncaught exception (group=0x40f82300)
12-10 17:37:58.980: E/AndroidRuntime(13574): FATAL EXCEPTION: main
12-10 17:37:58.980: E/AndroidRuntime(13574): java.lang.NullPointerException
12-10 17:37:58.980: E/AndroidRuntime(13574):    at no.whg.workout.MainActivity$MainFragment.onCreateView(MainActivity.java:189)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.support.v4.view.ViewPager.populate(ViewPager.java:895)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.support.v4.view.ViewPager.populate(ViewPager.java:772)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1234)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.view.View.measure(View.java:15172)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.view.View.measure(View.java:15172)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.view.View.measure(View.java:15172)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2435)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.view.View.measure(View.java:15172)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.view.Choreographer.doFrame(Choreographer.java:525)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.os.Handler.handleCallback(Handler.java:615)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.os.Looper.loop(Looper.java:137)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at android.app.ActivityThread.main(ActivityThread.java:4931)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at java.lang.reflect.Method.invokeNative(Native Method)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at java.lang.reflect.Method.invoke(Method.java:511)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
12-10 17:37:58.980: E/AndroidRuntime(13574):    at dalvik.system.NativeStart.main(Native Method)
  • 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-15T16:53:12+00:00Added an answer on June 15, 2026 at 4:53 pm

    If you look at the Fragment documentation, you’ll see that onCreateView() runs before onCreateActivity(). getActivity() will return null in onCreateView(). You need to move that piece out of onCreateView() and override onActivityCreated()..

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

Sidebar

Related Questions

Actually I have 2 Fragments, one is Menu and other is Content Fragment. Now
I have a Fragment activity that contains 6 fragments. Every fragment is instance of
I have a class that extends Fragment. At onCreateView I must request a JSON
I have an Activity that hosts multiple fragments using the actionbar's tab functionality. One
Have done some research and found some stuff that may be helpful. I would
I have done one program that allow user can listen to the music via
I have done some research and it really seems that implementing a transaction system
I have done small project in c++, the IDE I am using is Visual
i have an android app, that shows some fragments after swiping, the textView in
I have a menu button that starts a new activity and when it's done

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.