2

So I've decide to go back into android developing after dropping it for a bit. I Restarted making an old project in android studio I ran into a issue where I'm getting "cannot resolve symbol fragmentcontainer" and I'm sure it was working last time.

import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

public class MainActivity extends FragmentActivity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // add fragment to the activity
        FragmentManager fm = getSupportFragmentManager();

        // give fragment to manage
        Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);

        if (fragment == null) {
            fragment = new HomeFragment();
            fm.beginTransaction().add(R.id.fragmentContainer, fragment)
                    .commit();
        }
    }

}
stkent
  • 19,772
  • 14
  • 85
  • 111
Sam Kohnson
  • 115
  • 1
  • 3
  • 11

3 Answers3

1

It looks like your Activity layout R.layout.activity_main does not contain a view with id fragmentContainer. If that's not the issue, check this related question: Android Studio cannot resolve symbol but code executes correctly.

Community
  • 1
  • 1
stkent
  • 19,772
  • 14
  • 85
  • 111
0

just using "fragment_container" not "android.R.id.fragment_container" works for me...here is the detail

getFragmentManager().beginTransaction()
            .replace(fragment_container, new SettingsFragment())
            .commit();
0

I think you can use getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commitNow(); instead of using R.id.fragmentContainer to create fragment in an activity.

Wimukthi Rajapaksha
  • 961
  • 1
  • 11
  • 23