android - Open an activity with a fragment on it? -
i trying open activity menu, works, without problems. however, when put fragment on activity open, error
06-19 16:38:46.253 2732-2732/com.example.broodjepoep.stapvoorstap e/androidruntime﹕ fatal exception: main process: com.example.broodjepoep.stapvoorstap, pid: 2732 java.lang.runtimeexception: unable start activity componentinfo{com.example.broodjepoep.stapvoorstap/com.example.broodjepoep.stapvoorstap.layoutfragmentone}: android.view.inflateexception: binary xml file line #8: error inflating class fragment @ android.app.activitythread.performlaunchactivity(activitythread.java:2212) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2271) @ android.app.activitythread.access$800(activitythread.java:144) @ android.app.activitythread$h.handlemessage(activitythread.java:1205) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5146) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:732) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:566) @ dalvik.system.nativestart.main(native method) caused by: android.view.inflateexception: binary xml file line #8: error inflating class fragment @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:713) @ android.view.layoutinflater.rinflate(layoutinflater.java:755) @ 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.actionbaractivitydelegatebase.setcontentview(actionbaractivitydelegatebase.java:228) @ android.support.v7.app.actionbaractivity.setcontentview(actionbaractivity.java:102) @ com.example.broodjepoep.stapvoorstap.layoutfragmentone.oncreate(layoutfragmentone.java:15) @ android.app.activity.performcreate(activity.java:5231) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) @ android.app.activitythread.performlaunchactivity(activitythread.java:2169) at android.app.activitythread.handlelaunchactivity(activitythread.java:2271) at android.app.activitythread.access$800(activitythread.java:144) at android.app.activitythread$h.handlemessage(activitythread.java:1205) at android.os.handler.dispatchmessage(handler.java:102) at android.os.looper.loop(looper.java:136) at android.app.activitythread.main(activitythread.java:5146) at java.lang.reflect.method.invokenative(native method) at java.lang.reflect.method.invoke(method.java:515) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:732) at com.android.internal.os.zygoteinit.main(zygoteinit.java:566) at dalvik.system.nativestart.main(native method) caused by: java.lang.classcastexception: need implement senddata method @ com.example.broodjepoep.stapvoorstap.firstfragment.onattach(firstfragment.java:56) @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:849) @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1040) @ android.app.fragmentmanagerimpl.addfragment(fragmentmanager.java:1142) @ android.app.activity.oncreateview(activity.java:4786) @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:689) 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 android.support.v7.app.actionbaractivitydelegatebase.setcontentview(actionbaractivitydelegatebase.java:228) at android.support.v7.app.actionbaractivity.setcontentview(actionbaractivity.java:102) at com.example.broodjepoep.stapvoorstap.layoutfragmentone.oncreate(layoutfragmentone.java:15) at android.app.activity.performcreate(activity.java:5231) at android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) at android.app.activitythread.performlaunchactivity(activitythread.java:2169) at android.app.activitythread.handlelaunchactivity(activitythread.java:2271) at android.app.activitythread.access$800(activitythread.java:144) at android.app.activitythread$h.handlemessage(activitythread.java:1205) at android.os.handler.dispatchmessage(handler.java:102) at android.os.looper.loop(looper.java:136) at android.app.activitythread.main(activitythread.java:5146) at java.lang.reflect.method.invokenative(native method) at java.lang.reflect.method.invoke(method.java:515) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:732) at com.android.internal.os.zygoteinit.main(zygoteinit.java:566) at dalvik.system.nativestart.main(native method)
fwef
code behind menu item click
@override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } intent ione = new intent(this,layoutfragmentone.class); intent itwo = new intent(this,layoutfragmenttwo.class); switch(id) { case r.id.fragone: startactivity(ione); return true; case r.id.fragtwo: startactivity(itwo); return true; } return super.onoptionsitemselected(item); }
layoutfragmentone.class
package com.example.broodjepoep.stapvoorstap; import android.os.bundle; import android.support.v7.app.actionbaractivity; /** * created pizza on 19-6-2015. */ public class layoutfragmentone extends actionbaractivity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.layout_fragment_one); } }
layoutfragmenttwo.class
package com.example.broodjepoep.stapvoorstap; import android.os.bundle; import android.os.persistablebundle; import android.support.v7.app.actionbaractivity; /** * created pizza on 19-6-2015. */ public class layoutfragmenttwo extends actionbaractivity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.layout_fragment_two); } }
if put 2 fragments on mainactivity, there no error , can communicate between them without problems. problem occurs when try start activity with fragment on it
is known problem, or there known fix?
i dont know if needed, here code behind fragment 1 , fragment two
one:
package com.example.broodjepoep.stapvoorstap; import android.app.activity; import android.app.fragment; import android.os.bundle; import android.support.annotation.nullable; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.edittext; /** * created pizza on 19-6-2015. */ public class firstfragment extends fragment { sendmessage sm; button btnsenddata; edittext tb1; @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view v; v = inflater.inflate(r.layout.fragment_one,container,false); btnsenddata = (button)v.findviewbyid(r.id.btnsenddata); tb1 = (edittext)v.findviewbyid(r.id.tb1); btnsenddata.setonclicklistener(new button.onclicklistener() { @override public void onclick(view v) { string message; message = tb1.gettext().tostring(); sm.senddata(message); } }); return v; } interface sendmessage { public void senddata(string message); } @override public void onattach(activity activity) { super.onattach(activity); try { sm = (sendmessage) activity; } catch (classcastexception e) { throw new classcastexception("you need implement senddata method"); } } }
two:
package com.example.broodjepoep.stapvoorstap; import android.app.fragment; import android.os.bundle; import android.support.annotation.nullable; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview; /** * created pizza on 14-6-2015. */ public class secondfragment extends fragment { textview txt; @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v; v = inflater.inflate(r.layout.fragment_two,container,false); txt = (textview)v.findviewbyid(r.id.txt); return v; } public void getdata(string message) { txt.settext(message); } }
the error pretty clear:
caused by: java.lang.classcastexception: need implement senddata method
which happens when fragment being attached activity not implement said interface. it's written in code:
@override public void onattach(activity activity) { super.onattach(activity); try { sm = (sendmessage) activity; } catch (classcastexception e) { throw new classcastexception("you need implement senddata method"); } }
the activity wants use fragment such code, should implement interface:
public class layoutfragmentone extends actionbaractivity implements sendmessage { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.layout_fragment_one); } @override public void senddata(string message) { } }
Comments
Post a Comment