-1

I have just started with android fragment and git stuck while implementing an example from internet.Attaching the following code below.It would be really grate to get some help. I am getting a error at the line ft.add(R.id.fragmentone,fragment). Regards.

MainActivity.java

  import android.app.Fragment;
    import android.app.FragmentManager;
    import android.app.FragmentTransaction;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
        public void getfragment(View view)  {
            FragmentOne fragment = new FragmentOne();
            FragmentManager fm=getFragmentManager();
            FragmentTransaction ft=fm.beginTransaction();
            ft.add(R.id.fragmentone,fragment);
        }
    }

FragmentOne.java

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fragment_one, container,false);
    }
}

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.vighnesh.fragmentsexp.MainActivity"
    android:orientation="vertical">
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/buttonfragment"
   android:layout_margin="20dp"
    android:text="Click to open fragment"/>
    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragmentone"
        android:layout_margin="20dp">
    </fragment>
</LinearLayout>

fragment_fragment_one.xml

<FrameLayout 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"
    tools:context="com.example.vighnesh.fragmentsexp.FragmentOne">
    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />
</FrameLayout>
vighneshraje
  • 49
  • 1
  • 3

3 Answers3

0

Replace this code in your activity.

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

}

public void getfragment() {
    FragmentOne fragment = new FragmentOne();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.fragmentone, fragment);
    ft.commit();
}
0

change it.

 import android.app.Fragment;
    import android.app.FragmentManager;
    import android.app.FragmentTransaction;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    public class MainActivity extends AppCompatActivity {

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

        Button btnFragOne = (Button)findViewById(R.id.buttonfragment);
        btnFragOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                FragmentOne fragment = new FragmentOne();
                FragmentManager fm=getFragmentManager();
                FragmentTransaction ft=fm.beginTransaction();
                ft.add(R.id.fragmentone,fragment); getfragment();
            }
        });

    }
    }
Jai Khambhayta
  • 4,198
  • 2
  • 22
  • 29
0

So use getSupportFragmentManager() instead of getFragmentManager()

Because the FragmentOne is from the support library (android.support.v4.app.Fragment) not from the framework (android.app.Fragment), you need to use android.support.v4.app.FragmentManager to do transaction with FragmentOne.

Tin Tran
  • 2,265
  • 1
  • 14
  • 17