1

I was trying to inflate a fragment in an Activity and here is the code.

public class DetailActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_detail);
            if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container2, new DetailFragment()) 
                        .commit();    //Line with Error
            }
        }


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.detail, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_refresh) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }
    }

And here is my DetailFragment class

/**
 * A placeholder fragment containing a simple view.
 */
public class DetailFragment extends Fragment {

    public DetailFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
        return rootView;
    }
}

Here goes the layout for fragment_detail

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:textSize="18sp"
    android:id="@+id/fragment_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>

The error I am facing is cannot resolve method 'add(int,com.example...DetailFragment)'. As, I am beginner to android, please also guide me on how should I go about, tackling such type of errors.

Vikash Pandey
  • 5,407
  • 6
  • 41
  • 42
Panda
  • 2,400
  • 3
  • 25
  • 35
  • 1
    You've got the wrong `Fragment` class imported for `DetailFragment`. It should be `android.support.v4.app Fragment`. – Mike M. May 26 '16 at 17:28
  • I am sorry, but I didn't get u. – Panda May 26 '16 at 17:29
  • 1
    Possible duplicate of [Android: Adding a simple Fragment](http://stackoverflow.com/questions/25744681/android-adding-a-simple-fragment) – Mike M. May 26 '16 at 17:30
  • 2
    Look at the `import` statements in your `DetailFragment` class. The one that says `import android.app.Fragment;` should be `import android.support.v4.app.Fragment;` instead. – Mike M. May 26 '16 at 17:30

2 Answers2

9

Your DetailFragment need to extends from android.support.v4.app.Fragment instead of Fragment. Something like this

public class DetailFragment extends android.support.v4.app.Fragment {

 ......
}
Masum
  • 4,879
  • 2
  • 23
  • 28
0

Make sure your DetailFragment uses the fragment from the version 4 support library and import the library below into your fragment class. The problem is that you are using android.app.fragment instead of android.support.v4.app.Fragment.

import android.support.v4.app.Fragment;

public class DetailFragment extends Fragment{

}
Milad Bahmanabadi
  • 946
  • 11
  • 27
ali sampson
  • 321
  • 4
  • 7