3

There are a lot of articles pointing to the same error but none appeared to fix my problem. So I am posting it

I have a fragment layout below

<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=".MyAppointmentFragment" >

<!-- TODO: Update blank fragment layout -->

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/fragment_text" />

</FrameLayout>

And the activity layout below

 <LinearLayout 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:orientation="horizontal"
    android:baselineAligned="false"
    tools:context=".IndexActivity" 
   >
    <fragment 
    android:name="com.example.helloworld.MyAppointmentFragment" 
    android:id="@+id/myappointment_fragment"              
    android:layout_weight="1"              
    android:layout_width="0dp"              
    android:layout_height="match_parent" />
 </LinearLayout>

Below is the java source for the activity that is throwing this error

package com.example.helloworld;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class IndexActivity extends FragmentActivity {

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

package com.example.helloworld;

import android.app.Activity;
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;

/**
* A simple {@link android.support.v4.app.Fragment} subclass. Activities that
* contain this fragment must implement the
* {@link MyAppointmentFragment.OnFragmentInteractionListener} interface to
* handle interaction events. Use the {@link MyAppointmentFragment#newInstance}
* factory method to create an instance of this fragment.
* 
*/
public class MyAppointmentFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

/**
 * Use this factory method to create a new instance of this fragment using
 * the provided parameters.
 * 
 * @param param1
 *            Parameter 1.
 * @param param2
 *            Parameter 2.
 * @return A new instance of fragment MyAppointmentFragment.
 */
// TODO: Rename and change types and number of parameters
public static MyAppointmentFragment newInstance(String param1, String param2) {
    MyAppointmentFragment fragment = new MyAppointmentFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

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

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_my_appointment, 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;
}

/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated to
 * the activity and potentially other fragments contained in that activity.
 * <p>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    public void onFragmentInteraction(Uri uri);
}

  }

When launched in an emulator I get the following error

02-20 13:21:42.530: E/AndroidRuntime(1290): FATAL EXCEPTION: main
02-20 13:21:42.530: E/AndroidRuntime(1290): java.lang.RuntimeException: Unable to start   
activity   
ComponentInfo{com.example.helloworld/com.example.helloworld.IndexActivity}:  
android.view.InflateException: Binary XML file line #9: Error inflating class fragment
02-20 13:21:42.530: E/AndroidRuntime(1290):     at   
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)

The Android version I am using is 4.3 and minSdkVersion="13" and targetSdkVersion="18"

I would appreciate any help to resolve this issue

Complete Stack Trace Below

    FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld/com.example.helloworld.IndexActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
    at android.app.ActivityThread.access$600(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5103)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
    at android.app.Activity.setContentView(Activity.java:1895)
    at com.example.helloworld.IndexActivity.onCreate(IndexActivity.java:11)
    at android.app.Activity.performCreate(Activity.java:5133)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
    ... 11 more
 Caused by: java.lang.ClassCastException: com.example.helloworld.IndexActivity@4174c9f8 must implement OnFragmentInteractionListener
    at com.example.helloworld.MyAppointmentFragment.onAttach(MyAppointmentFragment.java:85)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:883)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
    ... 21 more
3
  • Please post the entire stack trace. Commented Feb 20, 2014 at 18:43
  • Modified the original post to include the complete stack trace. I noticed from the stacktrace that it is complaining on Commented Feb 20, 2014 at 20:21
  • must implement OnFragmentInteractionListener at com.example.helloworld.MyAppointmentFragment.onAttach Commented Feb 20, 2014 at 21:07

3 Answers 3

7
java.lang.ClassCastException: com.example.helloworld.IndexActivity@4174c9f8 must implement OnFragmentInteractionListener

Your IndexActivity class is supposed to implement some OnFragmentInteractionListener interface, and it does not.

Sign up to request clarification or add additional context in comments.

Comments

1

My answer is quite late cause I also faced the same problem and no solution was working for me. If none of the other areas like onFragmentInteractionListener() have problem then check for

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

as this method is deprecated replace it with

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

Comments

0

Make sure you have a string named fragment_text

android:text="@string/fragment_text" 

in your strings.xml file in the values folder. Even if you have, try doing a project 'clean' as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.