-1

Main Activity

package com.example.richu.fragment;
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 changeFragment(View view){
    Fragment fragment;
    if(view == findViewById(R.id.button3)){
        fragment = new FragmentOne();
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fragment1,fragment);
        ft.commit();
    }
    if(view == findViewById(R.id.button4)){
        fragment = new FragmentTwo();
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fragment1,fragment);
        ft.commit();
    }
}
}

MainActivity.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.richu.fragment.MainActivity">

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        tools:layout_editor_absoluteX="2dp"
        tools:layout_editor_absoluteY="5dp" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        tools:layout_editor_absoluteX="292dp"
        tools:layout_editor_absoluteY="5dp" />

    <fragment
        android:id="@+id/fragment1"
        android:name="layout.FragmentOne"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="13dp"
        tools:layout_editor_absoluteY="13dp" />
</LinearLayout>

FragmentOne.java

 package layout;
   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;

        import com.example.richu.fragment.R;


        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);
        }

        }

FragmentOne.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"
    android:background="@color/colorAccent"
    tools:context="layout.FragmentOne">


</FrameLayout>

FragmentTwo.java

package layout;

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;

import com.example.richu.fragment.R;

public class FragmentTwo 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_two, container, false);
    }


}

FragmentTwo.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"
    android:background="@color/colorAccent"
    tools:context="layout.FragmentTwo">


    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Hello" />
</FrameLayout>

I was trying to learn Fragment basics. Just added Two buttons in an activity. When one button is clicked, FragmentOne should load and when the second button is clicked, FragmentTwo should load. The following codes in MainActivity.java was showing errors and the error is Incompatible types. FragmentOne and FragmentTwo is the two Fragments that i created. - fragment = new FragmentOne(); - fragment = new FragmentTwo(); Please find a solution for this. Thank You.

2 Answers2

1

In Main Activity, you use import android.app.Fragment;

in FragmentTwo and FragmentOne You use import android.support.v4.app.Fragment;

You must decide with version is better for you.

Link show the difference: Difference between android.app.Fragment and android.support.v4.app.Fragment

Kamil Kulig
  • 91
  • 1
  • 6
0

The problem is that there is a mismatch in fragments types between objFragment and fragmentManager. The first is from package android.support.v4.app while the second is from the android.app package. To solve the problem change the getFragmentManager() to getSupportFragmentManager(). Just replace the code lines

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

to

    android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
GouravJn
  • 246
  • 1
  • 6
  • 18