Can not resolve method 'getSupportFragmentManager ()'

1

In my MainActivity.java where I am trying to put tabs, I am having the error Cannot resolve method 'getSupportFragmentManager()' and in my other activity with tab that I created when selecting "Tabbed Activity" at the time of creating, this error does not happen. I removed the code from this Tabbed Activity and embedded it in my code. How to correct?

activity_main.xml:

<android.support.constraint.ConstraintLayout
    android:id="@+id/constraintLayout2"
    android:layout_width="360dp"
    android:layout_height="511dp"
    ads:layout_constraintBottom_toBottomOf="parent"
    ads:layout_constraintEnd_toEndOf="parent"
    ads:layout_constraintStart_toStartOf="parent"
    ads:layout_constraintTop_toTopOf="parent"
    tools:context="com.example.mateuspc1.tab.TabActivity$PlaceholderFragment">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay"
        ads:layout_constraintStart_toStartOf="parent"
        ads:layout_constraintTop_toTopOf="parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_weight="1"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:title="@string/app_name">

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="360dp"
        android:layout_height="wrap_content"
        ads:layout_behavior="@string/appbar_scrolling_view_behavior"
        ads:layout_constraintStart_toStartOf="parent"
        ads:layout_constraintTop_toBottomOf="@+id/appbar">

    </android.support.v4.view.ViewPager>

    <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        ads:layout_constraintEnd_toEndOf="parent"
        ads:layout_constraintStart_toStartOf="parent"
        ads:layout_constraintTop_toBottomOf="@+id/appbar">

        <android.support.constraint.ConstraintLayout
            android:id="@+id/scrollcontainer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageButton
                android:id="@+id/vitasbut"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="@string/todo"
                android:src="@drawable/vitas1e2"
                ads:layout_constraintBottom_toTopOf="@+id/gtabut"
                ads:layout_constraintEnd_toStartOf="@+id/vitasbut2"
                ads:layout_constraintHorizontal_chainStyle="spread_inside"
                ads:layout_constraintStart_toStartOf="parent"
                ads:layout_constraintTop_toTopOf="parent" />

            <ImageButton
                android:id="@+id/vitasbut2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="@string/todo"
                android:src="@drawable/vitas2e2"
                ads:layout_constraintEnd_toStartOf="@+id/aiaichoquebut"
                ads:layout_constraintStart_toEndOf="@+id/vitasbut"
                ads:layout_constraintTop_toTopOf="parent" />

            <ImageButton
                android:id="@+id/gtabut"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="@string/todo"
                android:src="@drawable/gta1e2"
                ads:layout_constraintBottom_toTopOf="@+id/bolsbut"
                ads:layout_constraintEnd_toStartOf="@+id/gtabut2"
                ads:layout_constraintStart_toStartOf="parent"
                ads:layout_constraintTop_toBottomOf="@+id/vitasbut" />

            <ImageButton
                android:id="@+id/gtabut2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="@string/todo"
                android:src="@drawable/gta2e2"
                ads:layout_constraintEnd_toStartOf="@+id/acertomizbut"
                ads:layout_constraintStart_toEndOf="@+id/gtabut"
                ads:layout_constraintTop_toBottomOf="@+id/vitasbut2" />

            <ImageButton
                android:id="@+id/gabebut"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:contentDescription="@string/todo"
                android:src="@drawable/gabe1e2"
                ads:layout_constraintEnd_toEndOf="parent"
                ads:layout_constraintStart_toEndOf="@+id/gtabut2"
                ads:layout_constraintTop_toBottomOf="@+id/aiaichoquebut" />

            <ImageButton
                android:id="@+id/bolsbut"
                android:layout_width="0dp"
                android:layout_height="105dp"
                android:contentDescription="@string/todo"
                android:src="@drawable/bols1e2"
                ads:layout_constraintEnd_toStartOf="@+id/bolsbut2"
                ads:layout_constraintStart_toStartOf="parent"
                ads:layout_constraintTop_toBottomOf="@+id/gtabut" />

            <ImageButton
                android:id="@+id/bolsbut2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:contentDescription="@string/todo"
                android:src="@drawable/bols2e2"
                ads:layout_constraintEnd_toStartOf="@+id/aiaichoquebut"
                ads:layout_constraintStart_toEndOf="@+id/bolsbut"
                ads:layout_constraintTop_toBottomOf="@+id/gtabut2" />

            <ImageButton
                android:id="@+id/acertomizbut"
                android:layout_width="111dp"
                android:layout_height="0dp"
                android:src="@drawable/acertmiz1e2"
                ads:layout_constraintEnd_toEndOf="parent"
                ads:layout_constraintStart_toEndOf="@+id/bolsbut2"
                ads:layout_constraintTop_toBottomOf="@+id/gabebut" />

            <ImageButton
                android:id="@+id/aiaichoquebut"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/aiaichoque1e2"
                ads:layout_constraintBottom_toTopOf="@+id/gabebut"
                ads:layout_constraintEnd_toEndOf="parent"
                ads:layout_constraintStart_toEndOf="@+id/vitasbut2"
                ads:layout_constraintTop_toTopOf="parent" />

            <ImageButton
                android:id="@+id/bnsetimaartebut"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:src="@drawable/bnsetimaarte1e2"
                ads:layout_constraintStart_toStartOf="parent"
                ads:layout_constraintTop_toBottomOf="@+id/bolsbut" />

            <ImageButton
                android:id="@+id/fausteroubut"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/fausterou1e2"
                ads:layout_constraintStart_toEndOf="@+id/bnsetimaartebut"
                ads:layout_constraintTop_toBottomOf="@+id/onImageGalleryClicked" />

            <ImageButton
                android:id="@+id/fausttapegfogbut"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/faustfogo1e2"
                ads:layout_constraintStart_toStartOf="parent"
                ads:layout_constraintTop_toBottomOf="@+id/bnsetimaartebut" />

            <Button
                android:id="@+id/onImageGalleryClicked"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="24dp"
                android:layout_marginStart="24dp"
                android:layout_marginTop="24dp"
                android:text="@string/button"
                ads:layout_constraintStart_toEndOf="@+id/bnsetimaartebut"
                ads:layout_constraintTop_toBottomOf="@+id/bolsbut2" />

            <ImageView
                android:id="@+id/imgPicture"
                android:layout_width="53dp"
                android:layout_height="55dp"
                android:layout_marginStart="156dp"
                android:layout_marginTop="74dp"
                ads:layout_constraintStart_toEndOf="@+id/fausttapegfogbut"
                ads:layout_constraintTop_toBottomOf="@+id/acertomizbut" />

        </android.support.constraint.ConstraintLayout>


    </ScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        android:layout_marginStart="280dp"
        android:layout_marginTop="108dp"
        ads:layout_constraintStart_toStartOf="parent"
        ads:layout_constraintTop_toTopOf="parent"
        ads:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.constraint.ConstraintLayout>

<android.support.constraint.ConstraintLayout
    android:id="@+id/adViewConstraint"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    ads:layout_constraintEnd_toEndOf="parent"
    ads:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent">

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="320dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        app:adSize="BANNER"
        app:adUnitId="ca-app-pub-3940256099942544/6300978111"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.656"
        app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>

MainActivity.java (part of the code):

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        ViewPager mViewPager = findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

-

public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_tab, container, false); // fragment_tab alterado de activity_main
            TextView textView = rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }

    }

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

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

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }
    }

Ps: I'm a beginner.

    
asked by anonymous 01.03.2018 / 15:02

1 answer

3

Your main class is probably not FragmentActivity , the getSupportFragmentManager method is only supported on it.

Try using the method:

You can also change the class that is probably Activity , something like:

public class MainActivity extends Activity
{

By AppCompatActivity (already which AppCompatActivity extends from FragmentActivity )

public class MainActivity extends AppCompatActivity
{
    
01.03.2018 / 15:05