2
votes

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;

6
cast PlaceholderFragment() to Fragment. ...and I believe you don't have two classes in the same .java file, do you? - Alexander Zhak
Sorry i dont understand - ImAtWar
is PlaceholderFragment a v4 SupportFragment? - AndroidEnthusiast
can you show us the imports ? because you maybe using not the v4 fragment - Kiloreux

6 Answers

3
votes

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;

1
votes

Try this:

change getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
to 
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new PlaceholderFragment()).commit();
0
votes

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

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

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
votes

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