java - Android FATAL exception in Fragments -
i new android.i getting following error. have looked same questions , have applied answers code. still getting same error.
05-28 09:30:12.800 2016-2016/com.example.yatisawhney.fragmentdemo e/androidruntime: fatal exception: main process: com.example.yatisawhney.fragmentdemo, pid: 2016 java.lang.runtimeexception: unable start activity componentinfo{com.example.yatisawhney.fragmentdemo/com.example.yatisawhney.fragmentdemo.mainactivity}: android.view.inflateexception: binary xml file line #21: error inflating class fragment @ android.app.activitythread.performlaunchactivity(activitythread.java:2195) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) @ android.app.activitythread.access$800(activitythread.java:135) @ android.app.activitythread$h.handlemessage(activitythread.java:1196) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5017) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) @ dalvik.system.nativestart.main(native method) caused by: android.view.inflateexception: binary xml file line #21: error inflating class fragment @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:714) @ android.view.layoutinflater.rinflate(layoutinflater.java:756) @ android.view.layoutinflater.inflate(layoutinflater.java:492) @ android.view.layoutinflater.inflate(layoutinflater.java:397) @ android.view.layoutinflater.inflate(layoutinflater.java:353) @ android.support.v7.app.appcompatdelegateimplv7.setcontentview(appcompatdelegateimplv7.java:276) @ android.support.v7.app.appcompatactivity.setcontentview(appcompatactivity.java:136) @ com.example.yatisawhney.fragmentdemo.mainactivity.oncreate(mainactivity.java:13) @ android.app.activity.performcreate(activity.java:5231) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) @ android.app.activitythread.access$800(activitythread.java:135) @ android.app.activitythread$h.handlemessage(activitythread.java:1196) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5017) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) @ dalvik.system.nativestart.main(native method) caused by: java.lang.illegalstateexception: fragment com.example.yatisawhney.fragmentdemo.bottomfragment did not create view. @ android.support.v4.app.fragmentmanagerimpl.oncreateview(fragmentmanager.java:2319) @ android.support.v4.app.fragmentcontroller.oncreateview(fragmentcontroller.java:120) @ android.support.v4.app.fragmentactivity.dispatchfragmentsoncreateview(fragmentactivity.java:357) @ android.support.v4.app.basefragmentactivityhoneycomb.oncreateview(basefragmentactivityhoneycomb.java:31) @ android.support.v4.app.fragmentactivity.oncreateview(fragmentactivity.java:80) @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:690) @ android.view.layoutinflater.rinflate(layoutinflater.java:756) @ android.view.layoutinflater.inflate(layoutinflater.java:492) @ android.view.layoutinflater.inflate(layoutinflater.java:397) @ android.view.layoutinflater.inflate(layoutinflater.java:353) @ android.support.v7.app.appcompatdelegateimplv7.setcontentview(appcompatdelegateimplv7.java:276) @ android.support.v7.app.appcompatactivity.setcontentview(appcompatactivity.java:136) @ com.example.yatisawhney.fragmentdemo.mainactivity.oncreate(mainactivity.java:13) @ android.app.activity.performcreate(activity.java:5231) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) @ android.app.activitythread.access$800(activitythread.java:135) @ android.app.activitythread$h.handlemessage(activitythread.java:1196) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5017) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) @ dalvik.system.nativestart.main(native method)
the following java 3 files
mainactivity.java
package com.example.yatisawhney.fragmentdemo; import android.support.v7.app.appcompatactivity; import android.os.bundle; public class mainactivity extends appcompatactivity implements topfragment.activitycommunicator { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @override public void changetext(string a, string b) { bottomfragment bf = (bottomfragment)getsupportfragmentmanager().findfragmentbyid(r.id.fragment2); bf.setmeme(a,b); } }
bottom fragment
package com.example.yatisawhney.fragmentdemo; import android.os.bundle; import android.support.annotation.nullable; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview; /** * created yatisawhney on 28/05/16. */ public class bottomfragment extends fragment { private static textview top ; private static textview bottom; @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view bottom = inflater.inflate(r.layout.bottom_section ,container, false); top = (textview)bottom.findviewbyid(r.id.textview); bottom=(textview)bottom.findviewbyid(r.id.textview2); return bottom; } public void setmeme(string , string b){ top.settext(a); bottom.settext(b); } }
top fragment
package com.example.yatisawhney.fragmentdemo; import android.app.activity; import android.os.bundle; import android.support.annotation.nullable; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.edittext; /** * created yatisawhney on 28/05/16. */ public class topfragment extends fragment{ private static edittext top; private static edittext bottom; activitycommunicator communicator; public interface activitycommunicator{ public void changetext(string , string b); } private void changetext(){ communicator.changetext(top.gettext().tostring() , bottom.gettext().tostring()); } @override public void onattach(activity activity) { super.onattach(activity); try { communicator = (activitycommunicator) activity; }catch(exception e){ e.printstacktrace(); } } @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view top_view = inflater.inflate(r.layout.top_section , container , false); top=(edittext)top_view.findviewbyid(r.id.yates_text); bottom=(edittext)top_view.findviewbyid(r.id.yates_another_text); button button = (button)top_view.findviewbyid(r.id.yates_button); button.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { changetext(); } } ); return top_view; } }
activity_main.xml
<?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="com.example.yatisawhney.fragmentdemo.mainactivity"> <fragment android:layout_width="wrap_content" android:layout_height="wrap_content" android:name="com.example.yatisawhney.fragmentdemo.topfragment" android:id="@+id/fragment" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" tools:layout="@layout/top_section" /> <fragment android:layout_width="300dp" android:layout_height="300dp" android:name="com.example.yatisawhney.fragmentdemo.bottomfragment" android:id="@+id/fragment2" tools:layout="@layout/bottom_section" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" /> </relativelayout>
finally got answer question.
oncreate of fragment life cycle might called during oncreate of activity life cycle. not case times
therefore, on safer side if wish access views in fragment inside onactivitycreated() method. gives confirmation activity has been created , view hierarchy has been loaded memory. :)
Comments
Post a Comment