javascript - Can't go between fragments onClick -
i have 2 fragments, fragmenta , fragmentb. have button on layout of fragmenta, want go fragmentb when click button. tried few things doesn't work. have.
this fragmenta.
import android.app.fragmentmanager; import android.app.fragmenttransaction; import android.os.bundle; import android.app.fragment; //import android.support.v4.app.fragmentmanager; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.button; /** * simple {@link fragment} subclass. */ public class fragmenta extends fragment implements view.onclicklistener { button button; view v; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment v = inflater.inflate(r.layout.fragment_a, container, false); return v; } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); button= (button) getactivity().findviewbyid(r.id.clickme); } @override public void onclick(view v) { fragment fragment = new fragmentb(); fragmentmanager fragmentmanager = getfragmentmanager();; fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragmenttransaction.replace(r.layout.activity_main, fragment, fragment.tostring()); fragmenttransaction.addtobackstack(fragment.tostring()); fragmenttransaction.commit(); } }
this fragmentb.
import android.app.activity; import android.content.intent; import android.net.uri; import android.os.bundle; import android.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview; public class fragmentb extends fragment { textview textview; view v; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { v = inflater.inflate(r.layout.fragment_b, container, false); return v; } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); textview= (textview) getactivity().findviewbyid(r.id.textview); } }
this fragment_a.xml button inside it.
<framelayout 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" tools:context="net.cairobus.communication.fragmenta"> <!-- todo: update blank fragment layout --> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="new button" android:id="@+id/clickme" android:layout_gravity="center" /> </framelayout>
i can't figure out why won't work
button.setonclicklistener(this);
put below
button= (button) getactivity().findviewbyid(r.id.clickme);
Comments
Post a Comment