2 Replies - 556 Views - Last Post: 30 May 2017 - 06:30 AM

#1 bobj181  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-October 10

Help with Fragments

Posted 25 May 2017 - 08:27 AM

I am exploring the world of fragments and seem to not be getting far.

I have a simple Fragment XML file (created by the wizard in Android Studio):

<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"
    tools:context="com.example.a3johnr181.fragementtest.FragTestOne">

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



When I add the above fragment to my activity XML, it cause a fatal exception.

Here is the short version of run time exception:

Unable to start activity ComponentInfo{...MainActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class fragment

And this is embedded in the Exception text: Caused by: java.lang.RuntimeException: [email protected] must implement OnFragmentInteractionListener

I am guessing that it is blowing up in the onAttach method. Which is below:

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }



What I am not sure of is how to setup the call to the fragment to get the mListener field to be populated correctly. This is the call to the Fragment in the activity main xml.

    <fragment
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        android:id="@+id/FragOne"
        android:name="...FragTestOne"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="8dp" />



Thank you in advance for showing me the way...

Is This A Good Question/Topic? 0
  • +

Replies To: Help with Fragments

#2 abbieabbey6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-May 17

Re: Help with Fragments

Posted 29 May 2017 - 11:51 PM

Did you try to find the code in codota?
I think you will find it there for sure
Was This Post Helpful? 0
  • +
  • -

#3 bobj181  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-October 10

Re: Help with Fragments

Posted 30 May 2017 - 06:30 AM

abbieabbey6, thank you for the suggestion of the app, it looks like a great tool.

In digging through the error message more, what I found was that the activity calling the fragment must implement OnFragmentInteractionListener.

After implementing that it worked.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1