1

I have the following code:

package asus.example.com.notes1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ShareActionProvider;

public class MainActivity extends Activity {
private void selectItem(int position){
        Fragment fragment;
        switch (position){
            case 1:
                fragment = new PizzaFragment();
                break;
            case 2:
                fragment = new PastaFragment();
                break;
            case 3:
                fragment = new StoresFragment();
                break;
                default:
                    fragment = new TopFragment();
        }
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.content_frame, fragment);
        ft.addToBackStack(null);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.commit();
    }
}

I don't know why, but Android Studio underlines the following line: FragmentTransaction ft = getFragmentManager().beginTransaction(); and writes:

incompatible types. Required: android.support.v4.app.FragmentTransaction Found: android.app.FragmentTransaction

Tried to change:

android.support.v4.app.FragmentTransaction

on:

android.app.FragmentTransaction

But in such case, it undelines the following line:

ft.replace(R.id.content_frame, fragment);

and writes:

Wrong 2nd arguement type

Maybe, this is because of extends Activity (maybe everything 'd be normal with AppCompat, but I can't change, as if I know, theme which I use in my app isn't compatible with Appcompat)

aminography
  • 21,986
  • 13
  • 70
  • 74

1 Answers1

1

Mistake you are using android.support.v4.app.FragmentManager then you should use getSupportFragmentManager()

Need to change

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
Tung Duong
  • 1,156
  • 7
  • 19
  • but in such case it marks getSupportFragmentManager() and writes that method cannot be resolved – Sergei Mikhailovskii Sep 30 '18 at 17:20
  • You need make ‘MainActivity extends AppCompatActivity’ instead of Activity – Tung Duong Sep 30 '18 at 17:23
  • but I read that it'd cause problems with theme (I use Holo.Light.DarkActionBar). Or am I wrong? – Sergei Mikhailovskii Sep 30 '18 at 17:41
  • After change to AppCompatActivity you need to change theme to Theme.AppCompat also. See this https://stackoverflow.com/questions/21814825/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity – Tung Duong Oct 01 '18 at 01:16
  • but can I solve my problem without changing the theme? Or there's no other solution? – Sergei Mikhailovskii Oct 01 '18 at 15:42
  • Yes you can by keep using Activity, getFragmentManager, but need change `android.support.v4.app.Fragment` to `android.app.Fragment` and change `android.support.v4.app.FragmentTransaction` to `android.app.FragmentTransaction` – Tung Duong Oct 01 '18 at 15:49