0

Na minha MainActivity.java onde estou tentando colocar tabs, estou tendo o erro Cannot resolve method 'getSupportFragmentManager()' e na minha outra activity com tab que criei ao selecionar "Tabbed Activity" na hora de criar, este erro não acontece. Eu retirei o código dessa Tabbed Activity e incorporei em meu código. Como corrigir?

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 (parte do código):

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: Sou iniciante.

1 Resposta 1

2

Provavelmente a sua classe principal não é FragmentActivity, o método getSupportFragmentManager só é suportado nele.

Experimente usar o método:


Você também pode trocar a classe que provavelmente é Activity, algo como:

public class MainActivity extends Activity
{

Por AppCompatActivity (já que AppCompatActivity estende de FragmentActivity)

public class MainActivity extends AppCompatActivity
{
5
  • 1
    AppCompatActivity também suporta.
    – itscorey
    Commented 1/03/2018 às 14:07
  • @itscorey sim, porque ele extende de FragmentActivity, ai no caso o autor da pergunta poderia trocar a classe por FragmentActivity ou AppCompatActivity que já conseguiria resolver, mas respondi dentro do que ele já tinha, de qualquer forma vou editar a resposta.
    – Syzoth
    Commented 1/03/2018 às 14:25
  • Coloquei AppCompatActivity no lugar de Activity e funcionou. O engraçado é que eu tentei isso mais cedo, só que coloquei "AppCompactActivity" haha. Obrigado pela ajuda! =)
    – Asdra
    Commented 1/03/2018 às 15:01
  • @MateusScheper agora basta marcar a resposta como correta, se não souber fazer veja isto: pt.meta.stackoverflow.com/a/1079/3635
    – Syzoth
    Commented 1/03/2018 às 15:10
  • Feito! Até mais!
    – Asdra
    Commented 1/03/2018 às 16:00

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .