Sam Kohnson
Sam Kohnson

Reputation: 115

cannot resolve symbol fragmentcontainer

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();
        }
    }

}

Upvotes: 2

Views: 5030

Answers (3)

Wimukthi Rajapaksha
Wimukthi Rajapaksha

Reputation: 1019

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.

Upvotes: 0

bin mulyadin
bin mulyadin

Reputation: 1

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();

Upvotes: 0

stkent
stkent

Reputation: 20128

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.

Upvotes: 1

Related Questions