Sunday, 15 August 2010

android - getActivity causes the App to crash when called from a Fragment -



android - getActivity causes the App to crash when called from a Fragment -

i trying test screent orientation of device when using fragment larn how lifecycle of fragment work. in code posted below, in line

int rotation = getactivity().getwindowmanager().getdefaultdisplay().getrotation();

is using getactivity() optimal here? if yes, why app crashes when added it? please see logcat out set posted below.

java_code

public class fragment02 extends fragment { private final string tag = fragment02.class.getname(); private stringbuilder strbuilder = new stringbuilder(""); onclicklistener btn_00_listener = new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub // todo auto-generated method stub int rotation = getactivity().getwindowmanager().getdefaultdisplay().getrotation(); toast mtoast; switch(rotation) { case surface.rotation_0: mtoast = toast.maketext(getactivity(), r.string.portrait, toast.length_short); mtoast.setgravity(gravity.center, 10, 0); mtoast.show(); log.i(tag, "portrait"); break; case surface.rotation_90: mtoast = toast.maketext(getactivity(), r.string.landscape, toast.length_short); mtoast.setgravity(gravity.center, 10, 0); mtoast.show(); log.i(tag, "landscape"); break; case surface.rotation_180: mtoast = toast.maketext(getactivity(), r.string.reverse_portrait, toast.length_short); mtoast.setgravity(gravity.center, 10, 0); mtoast.show(); log.i(tag, "reverse_portrait"); break; case surface.rotation_270: mtoast = toast.maketext(getactivity(), r.string.reverse_landscape, toast.length_short); mtoast.setgravity(gravity.center, 10, 0); mtoast.show(); log.i(tag, "reverse_landscape"); break; default: log.i(tag, "reverse_portrait"); }// end switch } }; public void onattach(android.app.activity activity) { strbuilder.append("@onattached()./n"); log.i(tag, ""+strbuilder); }; @override public void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); strbuilder.append("@oncreate()./n"); log.i(tag, ""+strbuilder); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // todo auto-generated method stub strbuilder.append("@oncreateview()./n"); log.i(tag, ""+strbuilder); view v = inflater.inflate(r.layout.activity_screen_orientation_change, container, false); button btn_00 = (button) v.findviewbyid(r.id.btn_00); btn_00.setonclicklistener(btn_00_listener); homecoming v; }

logcat

10-25 12:52:19.550: e/androidruntime(31627): fatal exception: main 10-25 12:52:19.550: e/androidruntime(31627): process: com.example.viewpagerwithactiontabstest00, pid: 31627 10-25 12:52:19.550: e/androidruntime(31627): android.support.v4.app.supernotcalledexception: fragment fragment02{4291abe8 #1 id=0x7f080000 android:switcher:2131230720:1} did not phone call through super.onattach() 10-25 12:52:19.550: e/androidruntime(31627): @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:885) 10-25 12:52:19.550: e/androidruntime(31627): @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1104) 10-25 12:52:19.550: e/androidruntime(31627): @ android.support.v4.app.backstackrecord.run(backstackrecord.java:682) 10-25 12:52:19.550: e/androidruntime(31627): @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1467) 10-25 12:52:19.550: e/androidruntime(31627): @ android.support.v4.app.fragmentmanagerimpl.executependingtransactions(fragmentmanager.java:472) 10-25 12:52:19.550: e/androidruntime(31627): @ android.support.v4.app.fragmentpageradapter.finishupdate(fragmentpageradapter.java:141) 10-25 12:52:19.550: e/androidruntime(31627): @ android.support.v4.view.viewpager.populate(viewpager.java:1068) 10-25 12:52:19.550: e/androidruntime(31627): @ android.support.v4.view.viewpager.populate(viewpager.java:914) 10-25 12:52:19.550: e/androidruntime(31627): @ android.support.v4.view.viewpager.onmeasure(viewpager.java:1436) 10-25 12:52:19.550: e/androidruntime(31627): @ android.view.view.measure(view.java:17350) 10-25 12:52:19.550: e/androidruntime(31627): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5352) 10-25 12:52:19.550: e/androidruntime(31627): @ android.widget.framelayout.onmeasure(framelayout.java:310) 10-25 12:52:19.550: e/androidruntime(31627): @ android.view.view.measure(view.java:17350) 10-25 12:52:19.550: e/androidruntime(31627): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5352) 10-25 12:52:19.550: e/androidruntime(31627): @ com.android.internal.widget.actionbaroverlaylayout.onmeasure(actionbaroverlaylayout.java:382) 10-25 12:52:19.550: e/androidruntime(31627): @ android.view.view.measure(view.java:17350) 10-25 12:52:19.550: e/androidruntime(31627): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5352) 10-25 12:52:19.550: e/androidruntime(31627): @ android.widget.framelayout.onmeasure(framelayout.java:310) 10-25 12:52:19.550: e/androidruntime(31627): @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2505) 10-25 12:52:19.550: e/androidruntime(31627): @ android.view.view.measure(view.java:17350) 10-25 12:52:19.550: e/androidruntime(31627): @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:2175) 10-25 12:52:19.550: e/androidruntime(31627): @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1316) 10-25 12:52:19.550: e/androidruntime(31627): @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1513) 10-25 12:52:19.550: e/androidruntime(31627): @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1200) 10-25 12:52:19.550: e/androidruntime(31627): @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:6388) 10-25 12:52:19.550: e/androidruntime(31627): @ android.view.choreographer$callbackrecord.run(choreographer.java:803) 10-25 12:52:19.550: e/androidruntime(31627): @ android.view.choreographer.docallbacks(choreographer.java:603) 10-25 12:52:19.550: e/androidruntime(31627): @ android.view.choreographer.doframe(choreographer.java:573) 10-25 12:52:19.550: e/androidruntime(31627): @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:789) 10-25 12:52:19.550: e/androidruntime(31627): @ android.os.handler.handlecallback(handler.java:733) 10-25 12:52:19.550: e/androidruntime(31627): @ android.os.handler.dispatchmessage(handler.java:95) 10-25 12:52:19.550: e/androidruntime(31627): @ android.os.looper.loop(looper.java:157) 10-25 12:52:19.550: e/androidruntime(31627): @ android.app.activitythread.main(activitythread.java:5293) 10-25 12:52:19.550: e/androidruntime(31627): @ java.lang.reflect.method.invokenative(native method) 10-25 12:52:19.550: e/androidruntime(31627): @ java.lang.reflect.method.invoke(method.java:515) 10-25 12:52:19.550: e/androidruntime(31627): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1265) 10-25 12:52:19.550: e/androidruntime(31627): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1081) 10-25 12:52:19.550: e/androidruntime(31627): @ dalvik.system.nativestart.main(native method)

you weren't calling super's onattach method that's why error beingness thrown. you forgot @override annotation onattach method. third, should implement clicklistener instead of creating fellow member object.

try this:

public class fragment02 extends fragment implements view.onclicklistener { private final string tag = "fragment02"; @override public void onattach(activity activity) { super.onattach(activity); log.i(tag, "@onattach"); } @override public void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); log.i(tag, "@oncreate"); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { log.i(tag, "@oncreateview"); view v = inflater.inflate(r.layout.activity_screen_orientation_change, container, false); button btn_00 = (button) v.findviewbyid(r.id.btn_00); btn_00.setonclicklistener(this); homecoming v; } @override public void onclick(view view) { int rotation = getactivity().getwindowmanager().getdefaultdisplay().getrotation(); toast mtoast; switch(rotation) { case surface.rotation_0: mtoast = toast.maketext(getactivity(), r.string.portrait, toast.length_short); mtoast.setgravity(gravity.center, 10, 0); mtoast.show(); log.i(tag, "portrait"); break; case surface.rotation_90: mtoast = toast.maketext(getactivity(), r.string.landscape, toast.length_short); mtoast.setgravity(gravity.center, 10, 0); mtoast.show(); log.i(tag, "landscape"); break; case surface.rotation_180: mtoast = toast.maketext(getactivity(), r.string.reverse_portrait, toast.length_short); mtoast.setgravity(gravity.center, 10, 0); mtoast.show(); log.i(tag, "reverse_portrait"); break; case surface.rotation_270: mtoast = toast.maketext(getactivity(), r.string.reverse_landscape, toast.length_short); mtoast.setgravity(gravity.center, 10, 0); mtoast.show(); log.i(tag, "reverse_landscape"); break; default: log.i(tag, "reverse_portrait"); }// end switch } }

android android-fragments android-fragmentactivity android-listfragment android-lifecycle

No comments:

Post a Comment