0

I got this error when compiling the code below. Android studio also warned that Fragment is only supported at API level 11 and above.


Error:(16, 21) error: no suitable method found for add(int,ForecastFragment)
method FragmentTransaction.add(int,Fragment,String) is not applicable
(actual and formal argument lists differ in length)
method FragmentTransaction.add(int,Fragment) is not applicable
(actual argument ForecastFragment cannot be converted to Fragment by method invocation conversion)
method FragmentTransaction.add(Fragment,String) is not applicable
(actual argument int cannot be converted to Fragment by method invocation conversion)

Code:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new ForecastFragment())
                .commit();
    }
}


public class ForecastFragment extends Fragment {
private static final String TAG = ForecastFragment.class.getSimpleName();
private ArrayAdapter<String> mForecastAdapter;

public ForecastFragment() {
}
josliber
  • 43,891
  • 12
  • 98
  • 133
Peter Hua
  • 1
  • 1

1 Answers1

0

You should use the Fragment class from the support library. Check your imports section. Your code should look something like this:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.widget.ArrayAdapter;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new ForecastFragment())
                    .commit();
        }
    }

    public static class ForecastFragment extends Fragment {
        private final String TAG = ForecastFragment.class.getSimpleName();
        private ArrayAdapter<String> mForecastAdapter;

        public ForecastFragment() {
        }
    }
}
Mike
  • 4,550
  • 4
  • 33
  • 47
  • Awesome! This fixed it: "import android.support.v4.app.Fragment;" How is this different from "import android.app.Fragment;"? Just curious... – Peter Hua Apr 17 '15 at 15:20
  • [here](http://stackoverflow.com/questions/15109017/difference-between-android-app-fragment-and-android-support-v4-app-fragment) for more – Menelaos Kotsollaris Apr 18 '15 at 02:34