1

I have defined my fragments class as

import android.support.v4.app.Fragment;
public class UserAccountDetailsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.user_account_details_fragment, container, false);

}}

I have defined my activity as

   import android.support.v4.app.FragmentActivity;
   public class HomeScreenActivity extends FragmentActivity {
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_screen);
    UserAccountDetailsFragment userAccDetailsFrag = new   UserAccountDetailsFragment();

     android.app.FragmentManager fragmentManager = getFragmentManager();
      android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
      fragmentTransaction.add(R.id.fragments, userAccDetailsFrag );
      fragmentTransaction.commit();
    }

The problem is i am not able to add the fragment. It says UserAccountDetailsFragment should be made a fragment. But that class already extends fragments. What am i missing here?

XML---

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".HomeScreenActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<fragment 
    android:id="@+id/fragments"
      android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    />

D-D
  • 954
  • 2
  • 12
  • 27

5 Answers5

1

you are working with support.v4 package.
you should try the below:

FragmentManager manager=getSupportFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
tana
  • 585
  • 1
  • 5
  • 16
1

You simply cannot mix Fragment API from regular SDK with android-support SDK.

You have to call getSupportFragmentManager() instead getFragmentManager().

getSupportFragmentManager() returns android.support.v4.app.FragmentManager.

I would also suggest to check first if your fragment is already added and in case your fragment is NOT added to the back stack use setRetainInstance(true) on your fragment.

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class HomeScreenActivity extends FragmentActivity {

    private static final String FRAGMENT_TAG = "custom_tag";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_screen);
    Fragment frag = getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG);
    if (frag == null) {
        UserAccountDetailsFragment userAccDetailsFrag = new   UserAccountDetailsFragment();
        userAccDetailsFrag.setRetainInstance(true);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragments, userAccDetailsFrag, FRAGMENT_TAG);
        fragmentTransaction.commit();
    } 

}
Damian Petla
  • 8,963
  • 5
  • 44
  • 47
0

This is the problem:

android.app.FragmentManager fragmentManager = getFragmentManager(); android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

replace by:

android.support.v4.app.FragmentManager fragmentManager = getFragmentManager(); android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

and use this on in your xml:

<fragment
            android:id="@+id/map_relative_fragment"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment" />

and mention in manifest those:

 <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

and map key

Ravind Maurya
  • 977
  • 15
  • 24
  • What is the difference between the two? – D-D May 20 '14 at 09:16
  • when i changed from android.app.FragmentManager to android.support.v4.app.FragmentManager. i still get an error asking me to revert back – D-D May 20 '14 at 09:17
  • 1
    @D-D check you are using in your xml also this fragment support other wise it will gives you binary inflate xml error – Ravind Maurya May 20 '14 at 09:19
  • 1
    @D-D difference in those to discussed here :http://stackoverflow.com/questions/15109017/why-android-app-fragment-and-android-support-v4-app-fragment-is-conflicting – Ravind Maurya May 20 '14 at 09:21
0

try thi,

import android.support.v4.app.FragmentActivity;
   public class HomeScreenActivity extends FragmentActivity {
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_screen);
    UserAccountDetailsFragment userAccDetailsFrag = new   UserAccountDetailsFragment();

     android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
      android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
      fragmentTransaction.add(R.id.fragments, userAccDetailsFrag );
      fragmentTransaction.commit();
    }

this may help you

Akash Moradiya
  • 3,318
  • 1
  • 14
  • 19
0
      UserAccountDetailsFragment userAccDetailsFrag = new   UserAccountDetailsFragment();
                 FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
        .add(R.id.fragments, userAccDetailsFrag).commit();

try this

Ankit Bisht
  • 1,209
  • 12
  • 14
  • Eclipse is asking me to revert back to android.app.FragmentManager fragmentManager = getFragmentManager(); – D-D May 20 '14 at 09:27