android - java.lang.IllegalStateException: Can't change tag of fragment -
hi getting error while using pageviwer , adapter slide across 3 fragments.
here pageadapter
public class freedompageadapter extends fragmentpageradapter { private list<fragment> listfragment; public freedompageadapter(fragmentmanager fm, list<fragment> listfragment) { super(fm); this.listfragment = listfragment; } /* * (non-javadoc) * * @see android.support.v4.app.fragmentpageradapter#getitem(int) */ @override public fragment getitem(int position) { // todo auto-generated method stub return listfragment.get(position); } /* * (non-javadoc) * * @see android.support.v4.view.pageradapter#getcount() */ @override public int getcount() { // todo auto-generated method stub return listfragment.size(); }
here how define pageview
viewpager = (viewpager) findviewbyid(r.id.pager); viewpager.setonpagechangelistener(this); savedlistfragment = new subscribedfragment(); fragmentlist.add(savedlistfragment); fragmentlist.add(savedlistfragment); fragmentlist.add(savedlistfragment); viewpager.setadapter(new freedompageadapter(fragmentmanager(), fragmentlist)); private fragmentmanager fragmentmanager() { return getsupportfragmentmanager(); } private fragmenttransaction getfragmenttransaction() { return fragmentmanager().begintransaction(); }
page view listener
/* * (non-javadoc) * * @see android.support.v4.view.viewpager.onpagechangelistener# * onpagescrollstatechanged(int) */ @override public void onpagescrollstatechanged(int arg0) { } /* * (non-javadoc) * * @see * android.support.v4.view.viewpager.onpagechangelistener#onpagescrolled * (int, float, int) */ @override public void onpagescrolled(int arg0, float arg1, int arg2) { } /* * (non-javadoc) * * @see * android.support.v4.view.viewpager.onpagechangelistener#onpageselected * (int) */ @override public void onpageselected(int position) { actionbar.setselectednavigationitem(position); }
custom fragment object
public class subscribedfragment extends fragment{ /* (non-javadoc) * @see android.app.fragment#oncreate(android.os.bundle) */ @override public void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); } /* (non-javadoc) * @see android.app.fragment#oncreateview(android.view.layoutinflater, android.view.viewgroup, android.os.bundle) */ @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // todo auto-generated method stub return inflater.inflate(r.layout.discussion_list_layout, container); } }
full stack trace of error:
05-02 19:15:44.599: e/androidruntime(19839): fatal exception: main 05-02 19:15:44.599: e/androidruntime(19839): java.lang.illegalstateexception: can't change tag of fragment subscribedfragment{41157420 id=0x7f070005 android:switcher:2131165189:0}: android:switcher:2131165189:0 android:switcher:2131165189:1 05-02 19:15:44.599: e/androidruntime(19839): @ android.support.v4.app.backstackrecord.doaddop(backstackrecord.java:398) 05-02 19:15:44.599: e/androidruntime(19839): @ android.support.v4.app.backstackrecord.add(backstackrecord.java:389) 05-02 19:15:44.599: e/androidruntime(19839): @ android.support.v4.app.fragmentpageradapter.instantiateitem(fragmentpageradapter.java:99) 05-02 19:15:44.599: e/androidruntime(19839): @ android.support.v4.view.viewpager.addnewitem(viewpager.java:800) 05-02 19:15:44.599: e/androidruntime(19839): @ android.support.v4.view.viewpager.populate(viewpager.java:991) 05-02 19:15:44.599: e/androidruntime(19839): @ android.support.v4.view.viewpager.populate(viewpager.java:880) 05-02 19:15:44.599: e/androidruntime(19839): @ android.support.v4.view.viewpager.onmeasure(viewpager.java:1374) 05-02 19:15:44.599: e/androidruntime(19839): @ android.view.view.measure(view.java:15518) 05-02 19:15:44.599: e/androidruntime(19839): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4825) 05-02 19:15:44.599: e/androidruntime(19839): @ android.widget.framelayout.onmeasure(framelayout.java:310) 05-02 19:15:44.599: e/androidruntime(19839): @ android.view.view.measure(view.java:15518) 05-02 19:15:44.599: e/androidruntime(19839): @ android.widget.linearlayout.measurevertical(linearlayout.java:847) 05-02 19:15:44.599: e/androidruntime(19839): @ android.widget.linearlayout.onmeasure(linearlayout.java:588) 05-02 19:15:44.599: e/androidruntime(19839): @ android.view.view.measure(view.java:15518) 05-02 19:15:44.599: e/androidruntime(19839): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4825) 05-02 19:15:44.599: e/androidruntime(19839): @ android.widget.framelayout.onmeasure(framelayout.java:310) 05-02 19:15:44.599: e/androidruntime(19839): @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2434) 05-02 19:15:44.599: e/androidruntime(19839): @ android.view.view.measure(view.java:15518) 05-02 19:15:44.599: e/androidruntime(19839): @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:1874) 05-02 19:15:44.599: e/androidruntime(19839): @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1089) 05-02 19:15:44.599: e/androidruntime(19839): @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1265) 05-02 19:15:44.599: e/androidruntime(19839): @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:989) 05-02 19:15:44.599: e/androidruntime(19839): @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:4351) 05-02 19:15:44.599: e/androidruntime(19839): @ android.view.choreographer$callbackrecord.run(choreographer.java:749) 05-02 19:15:44.599: e/androidruntime(19839): @ android.view.choreographer.docallbacks(choreographer.java:562) 05-02 19:15:44.599: e/androidruntime(19839): @ android.view.choreographer.doframe(choreographer.java:532) 05-02 19:15:44.599: e/androidruntime(19839): @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:735) 05-02 19:15:44.599: e/androidruntime(19839): @ android.os.handler.handlecallback(handler.java:725) 05-02 19:15:44.599: e/androidruntime(19839): @ android.os.handler.dispatchmessage(handler.java:92) 05-02 19:15:44.599: e/androidruntime(19839): @ android.os.looper.loop(looper.java:137) 05-02 19:15:44.599: e/androidruntime(19839): @ android.app.activitythread.main(activitythread.java:5226) 05-02 19:15:44.599: e/androidruntime(19839): @ java.lang.reflect.method.invokenative(native method) 05-02 19:15:44.599: e/androidruntime(19839): @ java.lang.reflect.method.invoke(method.java:511) 05-02 19:15:44.599: e/androidruntime(19839): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:795) 05-02 19:15:44.599: e/androidruntime(19839): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:562) 05-02 19:15:44.599: e/androidruntime(19839): @ dalvik.system.nativestart.main(native method)
it's because you're adding same fragment instance 3 times list. should create new instance each page.
also, suggest looking fragmentstatepageradapter
if you're not far development. may better choice if want refresh content of fragments main activity.
Comments
Post a Comment