java - Error inflating class fragment While testing out fragment Activity -


i'm trying test out activity implement fragments.

when try run app error:

error inflating class fragment

ive read happens if extends activity instead of fragmentactivity in main activity, inherit right thing in activity itself. might 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 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 @ android.app.activitythread.performlaunchactivity(activitythread.java: 3253) @ android.app.activitythread.handlelaunchactivity(activitythread.java: 3349) @ android.app.activitythread.access$1100(activitythread.java: 221) @ android.app.activitythread$h.handlemessage(activitythread.java: 1794) @ android.os.handler.dispatchmessage(handler.java: 102) @ android.os.looper.loop(looper.java: 158) @ android.app.activitythread.main(activitythread.java: 7224) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java: 1230) @ 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 @ android.view.layoutinflater.inflate(layoutinflater.java: 551) @ android.view.layoutinflater.inflate(layoutinflater.java: 429) @ android.view.layoutinflater.inflate(layoutinflater.java: 380) @ com.android.internal.policy.phonewindow.setcontentview(phonewindow.java: 474) @ android.app.activity.setcontentview(activity.java: 2387) @ apps.radwin.zxingprojectfragmenttwo.mainactivity.oncreate(mainactivity.java: 15) @ android.app.activity.performcreate(activity.java: 6876) @ android.app.instrumentation.callactivityoncreate(instrumentation.java: 1135) @ android.app.activitythread.performlaunchactivity(activitythread.java: 3206) @ android.app.activitythread.handlelaunchactivity(activitythread.java: 3349)  @ android.app.activitythread.access$1100(activitythread.java: 221)  @ android.app.activitythread$h.handlemessage(activitythread.java: 1794)  @ android.os.handler.dispatchmessage(handler.java: 102)  @ android.os.looper.loop(looper.java: 158)  @ android.app.activitythread.main(activitythread.java: 7224)  @ java.lang.reflect.method.invoke(native method)  @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java: 1230)  @ com.android.internal.os.zygoteinit.main(zygoteinit.java: 1120)  caused by: android.view.inflateexception: binary xml file line #19: error inflating class fragment @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:794) @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:716) @ android.view.layoutinflater.rinflate(layoutinflater.java:847) @ android.view.layoutinflater.rinflatechildren(layoutinflater.java:810) @ android.view.layoutinflater.inflate(layoutinflater.java:527) @ android.view.layoutinflater.inflate(layoutinflater.java:429)  @ android.view.layoutinflater.inflate(layoutinflater.java:380)  @ com.android.internal.policy.phonewindow.setcontentview(phonewindow.java:474)  @ android.app.activity.setcontentview(activity.java:2387)  @ apps.radwin.zxingprojectfragmenttwo.mainactivity.oncreate(mainactivity.java:15)  @ android.app.activity.performcreate(activity.java:6876)  @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1135)  @ android.app.activitythread.performlaunchactivity(activitythread.java:3206)  @ android.app.activitythread.handlelaunchactivity(activitythread.java:3349)  @ android.app.activitythread.access$1100(activitythread.java:221)  @ android.app.activitythread$h.handlemessage(activitythread.java:1794)  @ android.os.handler.dispatchmessage(handler.java:102)  @ android.os.looper.loop(looper.java:158)  @ android.app.activitythread.main(activitythread.java:7224)  @ java.lang.reflect.method.invoke(native method)  @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1230)    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1120)  caused by: java.lang.runtimeexception: apps.radwin.zxingprojectfragmenttwo.mainactivity@44cf550 must implement onfragmentinteractionlistener @ apps.radwin.zxingprojectfragmenttwo.fragmentone.onattach(fragmentone.java:83) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1019) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1230) @ android.support.v4.app.fragmentmanagerimpl.addfragment(fragmentmanager.java:1332) @ android.support.v4.app.fragmentmanagerimpl.oncreateview(fragmentmanager.java:2288) @ android.support.v4.app.fragmentcontroller.oncreateview(fragmentcontroller.java:120) @ android.support.v4.app.fragmentactivity.dispatchfragmentsoncreateview(fragmentactivity.java:356) @ android.support.v4.app.basefragmentactivityhoneycomb.oncreateview(basefragmentactivityhoneycomb.java:31) @ android.support.v4.app.fragmentactivity.oncreateview(fragmentactivity.java:79) @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:766) @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:716)  @ android.view.layoutinflater.rinflate(layoutinflater.java:847)  @ android.view.layoutinflater.rinflatechildren(layoutinflater.java:810)  @ android.view.layoutinflater.inflate(layoutinflater.java:527)  @ android.view.layoutinflater.inflate(layoutinflater.java:429)  @ android.view.layoutinflater.inflate(layoutinflater.java:380)  @ com.android.internal.policy.phonewindow.setcontentview(phonewindow.java:474)  @ android.app.activity.setcontentview(activity.java:2387)  @ apps.radwin.zxingprojectfragmenttwo.mainactivity.oncreate(mainactivity.java:15)  @ android.app.activity.performcreate(activity.java:6876)  @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1135)  @ android.app.activitythread.performlaunchactivity(activitythread.java:3206)  @ android.app.activitythread.handlelaunchactivity(activitythread.java:3349)  @ android.app.activitythread.access$1100(activitythread.java:221)  @ android.app.activitythread$h.handlemessage(activitythread.java:1794)  @ android.os.handler.dispatchmessage(handler.java:102)  @ android.os.looper.loop(looper.java:158)  @ android.app.activitythread.main(activitythread.java:7224)  @ java.lang.reflect.method.invoke(native method)  @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1230)    @ 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;   /**  * simple {@link fragment} subclass.  * activities contain fragment must implement  * {@link fragmentone.onfragmentinteractionlistener} interface  * handle interaction events.  * use {@link fragmentone#newinstance} factory method  * create instance of fragment.  */ public class fragmentone extends fragment {     // todo: rename parameter arguments, choose names match     // fragment initialization parameters, e.g. arg_item_number     private static final string arg_param1 = "param1";     private static final string arg_param2 = "param2";      // todo: rename , change types of parameters     private string mparam1;     private string mparam2;      private onfragmentinteractionlistener mlistener;      public fragmentone() {         // required empty public constructor     }      /**      * use factory method create new instance of      * fragment using provided parameters.      *      * @param param1 parameter 1.      * @param param2 parameter 2.      * @return new instance of fragment fragmentone.      */     // todo: rename , change types , 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 layout fragment         return inflater.inflate(r.layout.fragment_fragment_one, container, false);     }      // todo: rename method, update argument , hook method 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;     }      /**      * interface must implemented activities contain      * fragment allow interaction in fragment communicated      * activity , potentially other fragments contained in      * activity.      * <p/>      * see android training lesson <a href=      * "http://developer.android.com/training/basics/fragments/communicating.html"      * >communicating other fragments</a> more information.      */     public interface onfragmentinteractionlistener {         // todo: update argument type , name         void onfragmentinteraction(uri uri);     } } 

the problem in fragment's onattach():

if (context instanceof onfragmentinteractionlistener) {     mlistener = (onfragmentinteractionlistener) context; } else {     throw new runtimeexception(context.tostring()         + " must implement onfragmentinteractionlistener"); } 

you're checking if context fragment attached (mainactivity in case) implements onfragmentinteractionlistener, not, runtimeexception thrown.

depending on achieve either remove code above onattach(), or make activity implement onfragmentinteractionlistener.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -