user2560571 user2560571 - 3 months ago 20
Android Question

Error inflating class fragment While testing out fragment Activity

I'm trying to test out an

Activity
that implement
Fragment
s.

When i try to run my app i get an error:


Error inflating class fragment


Ive read it happens if you extends the
Activity
instead of the
FragmentActivity
in the main
Activity
, but i inherit the right thing in the activity itself.
what might be the problem ?

my Activity :

package apps.radwin.zxingprojectfragmenttwo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;

public class MainActivity extends FragmentActivity {

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

public void selectFrag(View view) {
Fragment objFragment = new FragmentOne();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.fragment_Two_Id, objFragment)
.commit();
}
}


Activity Layout:

<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="apps.radwin.zxingprojectfragmenttwo.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView6" />


<fragment
android:id="@+id/fragment_One_Id"
android:name="apps.radwin.zxingprojectfragmenttwo.FragmentOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout="@layout/fragment_fragment_one"
android:layout_below="@+id/fragment_Two_Id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />


<fragment
android:id="@+id/fragment_Two_Id"
android:name="apps.radwin.zxingprojectfragmenttwo.FragmentTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout="@layout/fragment_fragment_two"
android:layout_below="@+id/textView6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity Button Change First"
android:id="@+id/button2"
android:onClick="selectFrag"
android:layout_below="@+id/fragment_One_Id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity Button Change Second"
android:id="@+id/button1"
android:onClick="selectFrag"
android:layout_below="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />


</RelativeLayout>


Full Stack Trace:

FATAL EXCEPTION: main
Process: apps.radwin.zxingprojectfragmenttwo, PID: 23530
java.lang.RuntimeException: Unable to start activity ComponentInfo {
apps.radwin.zxingprojectfragmenttwo / apps.radwin.zxingprojectfragmenttwo.MainActivity
}: android.view.InflateException: Binary XML file line #19: Binary XML file line # 19: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 3349)
at android.app.ActivityThread.access$1100(ActivityThread.java: 221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1794)
at android.os.Handler.dispatchMessage(Handler.java: 102)
at android.os.Looper.loop(Looper.java: 158)
at android.app.ActivityThread.main(ActivityThread.java: 7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 1120)
Caused by: android.view.InflateException: Binary XML file line #19: Binary XML file line # 19: Error inflating class fragment
at android.view.LayoutInflater.inflate(LayoutInflater.java: 551)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 429)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 380)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java: 474)
at android.app.Activity.setContentView(Activity.java: 2387)
at apps.radwin.zxingprojectfragmenttwo.MainActivity.onCreate(MainActivity.java: 15)
at android.app.Activity.performCreate(Activity.java: 6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 3349) 
at android.app.ActivityThread.access$1100(ActivityThread.java: 221) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1794) 
at android.os.Handler.dispatchMessage(Handler.java: 102) 
at android.os.Looper.loop(Looper.java: 158) 
at android.app.ActivityThread.main(ActivityThread.java: 7224) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 1120) 
Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:380) 
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:474) 
at android.app.Activity.setContentView(Activity.java:2387) 
at apps.radwin.zxingprojectfragmenttwo.MainActivity.onCreate(MainActivity.java:15) 
at android.app.Activity.performCreate(Activity.java:6876) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7224) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Caused by: java.lang.RuntimeException: apps.radwin.zxingprojectfragmenttwo.MainActivity@44cf550 must implement OnFragmentInteractionListener
at apps.radwin.zxingprojectfragmenttwo.FragmentOne.onAttach(FragmentOne.java:83)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1019)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:356)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:766)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:847) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:527) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:380) 
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:474) 
at android.app.Activity.setContentView(Activity.java:2387) 
at apps.radwin.zxingprojectfragmenttwo.MainActivity.onCreate(MainActivity.java:15) 
at android.app.Activity.performCreate(Activity.java:6876) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7224) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 


my FragmentOne Class:

package apps.radwin.zxingprojectfragmenttwo;

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;


/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link FragmentOne.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link FragmentOne#newInstance} factory method to
* create an instance of this fragment.
*/
public class FragmentOne 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;

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

/**
* 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 FragmentOne.
*/
// TODO: Rename and change types and number of parameters
public static FragmentOne newInstance(String param1, String param2) {
FragmentOne fragment = new FragmentOne();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

@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_fragment_one, 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(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.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
void onFragmentInteraction(Uri uri);
}
}

Answer

The problem is in your Fragment's onAttach():

if (context instanceof OnFragmentInteractionListener) {
    mListener = (OnFragmentInteractionListener) context;
} else {
    throw new RuntimeException(context.toString()
        + " must implement OnFragmentInteractionListener");
}

You're checking if the Context the Fragment is attached to (MainActivity in this case) implements OnFragmentInteractionListener, but it does not, so a RuntimeException is thrown.

Depending on what you would like to achieve either remove the code above from your onAttach(), or make your Activity implement OnFragmentInteractionListener.