ImAtWar ImAtWar - 2 months ago 23
Android Question

Cannot Resolve method Android Studio

I am getting an error on this code using android studio


Cannot resolve method 'add(int, android.trey.startingpoint.nl.SecondActivity.PlaceHolderFragment)'


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}

public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_second, container, false);
return rootView;
}
}


In seperate posts i found out that when you delete the if part, things should roll fine, except that it throws another error.

Thanks for the help!

Edit:Imports:


import android.app.Activity;
import android.support.v4.app.Fragment;

import android.os.Bundle; import android.view.LayoutInflater; import
android.view.Menu; import android.view.MenuItem; import
android.view.View; import android.view.ViewGroup; import
android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory; import
com.google.android.gms.maps.GoogleMap; import
com.google.android.gms.maps.MapFragment; import
com.google.android.gms.maps.model.BitmapDescriptorFactory; import
com.google.android.gms.maps.model.CameraPosition; import
com.google.android.gms.maps.model.LatLng; import
com.google.android.gms.maps.model.MarkerOptions;

Answer

PlaceholderFragment might be a support fragment.

Change getFragmentManager to getSupportFragmentManager() as below:

if (savedInstanceState == null) {
    getSupportFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment()).commit();
}

Your activity must also extend FragmentActivity.

Note:

If you don't intend on using support fragments then you can just change the import for the fragments.

Change import android.support.v4.app.Fragment; to import android.app.Fragment;