android - Changing the fragmentmanager not recognize the buttons and fails -
very good. met problem had when declaring buttons on other fragments contain fragment. forum , @shudy solve problem. last post: when declaring buttons different fragments not recognize
but needs had change way had embedded fragment of buttons , when run app. error not how fix.
this code modified
public class grp1fragment extends fragment { private int contarrayask = 0; private int contright = 0; private int contfailed= 0; private button buttontrue; private button buttonfalse; private button buttonnextask; private button buttonsharescore; private view view; string[] arrayfragmentaresultsgrp1 = new string[]{"0","1","0","1","0","1","0","1","0","1",}; private fragment[] fragmentschangeask = new fragment[]{ new grp1fragmentp1(), new grp1fragmentp2(), new grp1fragmentp3(), }; private fragment[] fragmentschangebutton = new fragment[]{ new trueofalsefragment(), new nextaskfragment(), new sharescorefragment(), }; public grp1fragment() { // required empty public constructor } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view = inflater.inflate(r.layout.fragment_grp1, container, false); android.support.v4.app.fragmentmanager managerask = getactivity().getsupportfragmentmanager(); managerask.begintransaction() .add(r.id.fragmentaskgrp1, fragmentschangeask[0]) .add(r.id.fragmentaskgrp1, fragmentschangeask[1]) .add(r.id.fragmentaskgrp1, fragmentschangeask[2]) .hide(fragmentschangeask[1]) .hide(fragmentschangeask[2]) .commit(); android.support.v4.app.fragmentmanager managerbutton = getactivity().getsupportfragmentmanager(); managerbutton.begintransaction() .add(r.id.fragmentbutton, fragmentschangebutton[0]) .add(r.id.fragmentbutton, fragmentschangebutton[1]) .add(r.id.fragmentbutton, fragmentschangebutton[2]) .hide(fragmentschangebutton[1]) .hide(fragmentschangebutton[2]) .commit(); buttontrue = (button) view.findviewbyid(r.id.buttontrue); buttontrue.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { toast.maketext(getactivity(), "you pressed button: true", toast.length_long).show(); //contarrayask++; //setcontent(contarrayask); } }); buttonfalse = (button) view.findviewbyid(r.id.buttonfalse); /*buttonfalse.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { toast.maketext(getactivity(), "ha pulsado el botón falso", toast.length_long).show(); } });*/ buttonnextask = (button) view.findviewbyid(r.id.buttonnextask); buttonsharescore = (button) view.findviewbyid(r.id.buttonsharescore); /*buttonnextask.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { toast.maketext(getactivity(), "ha pulsado el botón siguiente pregunta", toast.length_long).show(); } });*/ return view; } public void setcontent(int index){ fragment tohide1 = null; fragment tohide2 = null; fragment tohide3 = null; fragment tohide4 = null; fragment tohide5 = null; fragment tohide6 = null; fragment tohide7 = null; fragment tohide8 = null; fragment tohide9 = null; fragment toshow = null; switch (index){ case 0: tohide1 = fragmentschangeask[1]; tohide2 = fragmentschangeask[2]; toshow = fragmentschangeask[0]; break; case 1: tohide1 = fragmentschangeask[0]; tohide2 = fragmentschangeask[2]; toshow = fragmentschangeask[1]; break; case 2: tohide1 = fragmentschangeask[1]; tohide2 = fragmentschangeask[0]; toshow = fragmentschangeask[2]; break; } android.support.v4.app.fragmentmanager managerask = getactivity().getsupportfragmentmanager(); managerask.begintransaction() .hide(tohide1) .hide(tohide2) .show(toshow) .commit(); } }
the error when uncommented following line of code:
buttontrue.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { toast.maketext(getactivity(), "you pressed button: true", toast.length_long).show(); //contarrayask++; //setcontent(contarrayask); } });
the layout follows:
<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:baselinealigned="false"> <framelayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/fragmentbutton" android:layout_alignparenttop="true" android:layout_alignparentleft="true" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/grp1" android:id="@+id/textgrp1" android:textsize="25sp" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="5dp"/> <framelayout android:id="@+id/fragmentaskgrp1" android:layout_width="match_parent" android:layout_height="450dp" android:layout_below="@+id/textgrp1" android:layout_centerhorizontal="true" android:layout_margintop="5dp"/> </relativelayout>
the layout have buttons:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fragment_trueorfalse"> <button android:layout_width="120sp" android:layout_height="wrap_content" android:textallcaps="false" android:text="@string/buttontrue" android:id="@+id/buttontrue" android:layout_marginleft="40dp" android:layout_alignparentbottom="true" android:layout_alignparentleft="true" /> <button android:layout_width="120sp" android:layout_height="wrap_content" android:textallcaps="false" android:text="@string/buttonfalse" android:id="@+id/buttonfalse" android:layout_marginright="40dp" android:layout_alignparentbottom="true" android:layout_alignparentright="true" /> </relativelayout>
and error gives me logcat:
04-01 11:43:58.279 492-492/josejuansosarodriguez.radioecca.conocecanarias e/androidruntime﹕ uncaught handler: thread main exiting due uncaught exception 04-01 11:43:58.289 492-492/josejuansosarodriguez.radioecca.conocecanarias e/androidruntime﹕ java.lang.runtimeexception: unable start activity componentinfo{josejuansosarodriguez.radioecca.conocecanarias/josejuansosarodriguez.radioecca.conocecanarias.mainactivity}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:2496) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2512) @ android.app.activitythread.access$2200(activitythread.java:119) @ android.app.activitythread$h.handlemessage(activitythread.java:1863) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:123) @ android.app.activitythread.main(activitythread.java:4363) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:521) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:860) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:618) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ fragmentsdrawer.grp1fragment.oncreateview(grp1fragment.java:87) @ android.support.v4.app.fragment.performcreateview(fragment.java:1786) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:947) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1126) @ android.support.v4.app.backstackrecord.run(backstackrecord.java:739) @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1489) @ android.support.v4.app.fragmentactivity.onstart(fragmentactivity.java:548) @ android.app.instrumentation.callactivityonstart(instrumentation.java:1129) @ android.app.activity.performstart(activity.java:3723) @ android.app.activitythread.performlaunchactivity(activitythread.java:2468) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2512) @ android.app.activitythread.access$2200(activitythread.java:119) at android.app.activitythread$h.handlemessage(activitythread.java:1863) at android.os.handler.dispatchmessage(handler.java:99) at android.os.looper.loop(looper.java:123) at android.app.activitythread.main(activitythread.java:4363) at java.lang.reflect.method.invokenative(native method) at java.lang.reflect.method.invoke(method.java:521) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:860) at com.android.internal.os.zygoteinit.main(zygoteinit.java:618) at dalvik.system.nativestart.main(native method) 04-01 11:43:58.299 492-495/josejuansosarodriguez.radioecca.conocecanarias i/dalvikvm﹕ threadid=7: reacting signal 3 04-01 11:43:58.309 492-495/josejuansosarodriguez.radioecca.conocecanarias e/dalvikvm﹕ unable open stack trace file '/data/anr/traces.txt': permission denied
i hope have spread far. thank everything, forum amazing.
with fragments:
inflate , return view in oncreateview()
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view = inflater.inflate(r.layout.fragment_grp1, container, false); return view; }
then reference view elements in onviewcreated() , add listeners:
@override public void onviewcreated(view view, bundle savedinstancestate){ super.onviewcreated(view, savedinstancestate); buttontrue = (button) view.findviewbyid(r.id.buttontrue); buttontrue.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { toast.maketext(getactivity(), "you pressed button: true", toast.length_long).show(); //contarrayask++; //setcontent(contarrayask); } }); buttonfalse = (button) view.findviewbyid(r.id.buttonfalse); /*buttonfalse.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { toast.maketext(getactivity(), "ha pulsado el botón falso", toast.length_long).show(); } });*/ buttonnextask = (button) view.findviewbyid(r.id.buttonnextask); buttonsharescore = (button) view.findviewbyid(r.id.buttonsharescore); /*buttonnextask.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { toast.maketext(getactivity(), "ha pulsado el botón siguiente pregunta", toast.length_long).show(); } });*/ }
then perform heavier operations once onactivitycreated callback received.
@override public void onactivitycreated(bundle savedinstancestate){ super.onactivitycreated(savedinstancestate); android.support.v4.app.fragmentmanager managerask = getactivity().getsupportfragmentmanager(); managerask.begintransaction() .add(r.id.fragmentaskgrp1, fragmentschangeask[0]) .add(r.id.fragmentaskgrp1, fragmentschangeask[1]) .add(r.id.fragmentaskgrp1, fragmentschangeask[2]) .hide(fragmentschangeask[1]) .hide(fragmentschangeask[2]) .commit(); android.support.v4.app.fragmentmanager managerbutton = getactivity().getsupportfragmentmanager(); managerbutton.begintransaction() .add(r.id.fragmentbutton, fragmentschangebutton[0]) .add(r.id.fragmentbutton, fragmentschangebutton[1]) .add(r.id.fragmentbutton, fragmentschangebutton[2]) .hide(fragmentschangebutton[1]) .hide(fragmentschangebutton[2]) .commit(); }
additionally should checking make sure view elements not null before doing operations them...
ex..
if(buttontrue != null){ buttontrue.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { toast.maketext(getactivity(), "you pressed button: true", toast.length_long).show(); //contarrayask++; //setcontent(contarrayask); } }); }
Comments
Post a Comment