7

I am pretty new to Android Apps, so I hope I can find some help here. I have already searched for my problem in here and found something, but that does not work.

I want to add a Fragment to a FrameLayout but it does not work. My goal ist to create a Frame (/Framework?) that is always present and the user can interact with it and inside this Frame in a specific "window" I want to display pages/fragments, five in total, and beeing able to switch the pages/fragments at any time, so I have an always present Frame and inside this dynamically changing pages. But for now I am stuck at the very beginning with adding a simple fragment to this Frame (which is already working btw.)

This is all the relevant code I hope: The error occurs in the MainActivity.java (getSupportFragmentManager().beginTransaction().add(R.id.mainFrame, homeFragment).commit();) where it tells me:

Error:(25, 55) error: no suitable method found for add(int,HomeFragment) method FragmentTransaction.add(Fragment,String) is not applicable (argument mismatch; int cannot be converted to Fragment) method FragmentTransaction.add(int,Fragment) is not applicable (argument mismatch; HomeFragment cannot be converted to Fragment)

I already tried to cast homeFragment to Fragment, but that did not work.

MainActivity.java

import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;


public class MainActivity extends FragmentActivity
{
    FragmentTransaction fragmentTransaction;
    HomeFragment homeFragment;

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

        homeFragment = new HomeFragment();
        **getSupportFragmentManager().beginTransaction().add(R.id.mainFrame, homeFragment).commit();**
}

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame" >

    <FrameLayout
        android:id = "@+id/mainFrame"
        android:layout_width = "match_parent"
        android:layout_height = "match_parent"
        android:layout_marginBottom = "@dimen/bottom_Main_Tabs">
        </FrameLayout>

    [...]

</FrameLayout>

fragment_home.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.HomeFragment"> // it is not really com.example...

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

HomeFragment.java (everything is autogenerated yet, but I already cut something out)

public class HomeFragment extends Fragment
{
    private OnFragmentInteractionListener mListener;


    public static HomeFragment newInstance()
    {
        HomeFragment fragment = new HomeFragment();
        return fragment; // not really neccessary, because it Have shortened it
    }

    public HomeFragment()
    {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri)
    {
        if (mListener != null)
        {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        try
        {
            mListener = (OnFragmentInteractionListener) activity;
        }
        catch (ClassCastException e)
        {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach()
    {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener
    {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }

}

Can somebody please help me?

John

JRsz
  • 2,891
  • 4
  • 28
  • 44

1 Answers1

19

You're mixing up classes from the support library and the new classes available only to newer versions of the OS.

For example, you import android.app.FragmentTransaction (available for API 11+) but the call to getSupportFragmentManager().beginTransaction() returns android.support.v4.app.FragmentTransaction ...

You need to import android.support.v4.app.FragmentTransaction and make sure that your HomeFragment extends android.support.v4.app.Fragment and not android.app.Fragment.

2Dee
  • 8,609
  • 7
  • 42
  • 53
  • 1
    First thanks, the compiler was fine with this. But if i understood it correctly, this is the way so older Versions can work with it. My minSDK is higher, so I would like to do it without the support library. What exactly would I have to do then? And if I run the app, it crashes with this message: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MainActivity}: java.lang.ClassCastException: com.example.app.MainActivity@b1d28ff0 must implement OnFragmentInteractionListener – JRsz Sep 09 '14 at 12:57
  • 1
    Then have your Activity extend Activity instead of FragmentActivity (you are still able to work with Fragments, provided that you have minSdk 11 or more, FragmentActivity is only there to give devs the ability to use Fragments with older OS versions), your Fragment should extend android.app.Fragment and you should call getFragmentManager in your Activity instead of getSupportFragmentManager. – 2Dee Sep 09 '14 at 13:02
  • As for the exception, look at your code, it's from your HomeFragment, it requires the Activity adding the Fragment to implement the onFragmentInteraction method. I guess you just copy/pasted code from somewhere, so here's a link to the documentation page that goes with the code you're using : http://developer.android.com/training/basics/fragments/communicating.html – 2Dee Sep 09 '14 at 13:06
  • Yes, that is the exact same link I found on my own and everything works perfectly finde now :) Thanks a lot, in combination with the link you provided, my question is entirely solved :D (for now ^^) – JRsz Sep 09 '14 at 13:25
  • Can you help me also on this one? http://stackoverflow.com/questions/25753953/android-error-with-on-fragment-interaction-listener I cannot chat yet, so I have to use the comments... – JRsz Sep 09 '14 at 22:18