Cannot resolve method 'getSupportFragmentManager()'

Asked

Viewed 233 times

0

In my Mainactivity.java where I am trying to put tabs, I am having the error Cannot resolve method 'getSupportFragmentManager()' and in my other tab Activity that I created when selecting "Tabbed Activity" when creating, this error does not happen. I removed the code from this Tabbed Activity and incorporated it into my code. How to fix?

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 am beginner.

1 answer

2


Probably his main class is not FragmentActivity, the method getSupportFragmentManager 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
{

For AppCompatActivity (since AppCompatActivity extension of FragmentActivity)

public class MainActivity extends AppCompatActivity
{
  • 1

    Appcompatactivity also supports.

  • @itscorey yes, because it extends from FragmentActivity, in case the author of the question could exchange the class for FragmentActivity or AppCompatActivity that I could already solve, but I answered inside of what he already had, anyway I will edit the answer.

  • I put Appcompatactivity in place of Activity and it worked. The funny thing is I tried this earlier, only I put "Appcompactactivity" haha. Thanks for the help! =)

  • @Mateusscheper now just mark the answer as correct, if you can’t do it see this: https://pt.meta.stackoverflow.com/a/1079/3635

  • Done! See you later!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.