2

How can I resolve this error? I am getting an error on this code:

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

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;

ImAtWar
  • 1,073
  • 3
  • 11
  • 23

6 Answers6

3

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;

AndroidEnthusiast
  • 6,557
  • 10
  • 42
  • 56
  • if that's the case fixing the imports will be easier for him – Kiloreux Sep 29 '14 at 14:03
  • Yea i found out about this, but when i change that, i get an error saying Cannot Resolve method get supportFragmentManager, And if i import the import android.support.v4.app.FragmentManager; it just stays unused – ImAtWar Sep 29 '14 at 14:04
  • @ImAtWar please you post your imports – AndroidEnthusiast Sep 29 '14 at 14:05
  • edited. But since this is a second Activity, i have more imports used in the main activity – ImAtWar Sep 29 '14 at 14:06
  • change import android.support.v4.app.Fragment; to import android.app.Fragment; – AndroidEnthusiast Sep 29 '14 at 14:08
  • Thx, it seems to work.. Tell me if im wrong, but is the Support an update for froyo? For older devices to work? What if i wanted to use the support manager, what would be the solution? – ImAtWar Sep 29 '14 at 14:12
  • in a way yes, the support library provides backward compatibility. If you wanted to use support manager then you would have to change getFragmentManager to getSupportFragmentManager – AndroidEnthusiast Sep 29 '14 at 14:21
  • So if i use the Support Fragment, do i need an empty, private Fragmentmanager getSupportFragmentManager? – ImAtWar Sep 29 '14 at 14:29
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62120/discussion-between-androidenthusiast-and-imatwar). – AndroidEnthusiast Sep 29 '14 at 14:33
1

Try this:

change getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
to 
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new PlaceholderFragment()).commit();
QArea
  • 4,955
  • 1
  • 12
  • 22
0

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

like

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().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_main, container, false);
            return rootView;
        }
    }
}
ZoroShi
  • 21
  • 2
0

in your fragment file using import android.support.v4.app.Fragment;

rather than import android.app.Fragment;

than can fix it;

Here is example:

In your activity file:

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

    TravelMessageFragment fragment = new TravelMessageFragment();
    getSupportFragmentManager().beginTransaction().add(R.id.travelInit,fragment).commit();

}

than in your fragment file:

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TravelMessageFragment extends Fragment{
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_travel_message, group, false);

    return view;
}
}
庄景鹏
  • 960
  • 9
  • 7
-1

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

Change two things

First Thing :

you can create object of class "android.trey.startingpoint.nl.SecondActivity.PlaceHolderFragment" instead of putting class name

like

if(findViewById(R.id.fragment_container) != null) {

if(savedInstanceState != null) {

return;

}
PlaceHolderFragment mFragment = new PlaceHolderFragment();

mFragment.setArguments(getIntent().getExtras());

getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,th
irdFragment).commit();

}

Second Thing:

Go to class file PlaceHolderFragment and change import " android.support.v4.app.Fragment" from "android.support.app.Fragment"

-2

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

and also getSupportFragmentManager() to

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

it works for me

Dave Bennett
  • 10,996
  • 3
  • 30
  • 41