java - putExtra() not working for Button:onClick -
i have 2 activities , i'm trying put values first 1 second one. in first activity, created button onclick parameter.
<button android:id="@+id/navibutton" android:layout_width="match_parent" android:layout_height="60dp" android:text="navigate" android:layout_alignparentbottom="true" android:onclick="startnavigation"/>
then created 2 methods in activity selecteddock: oncreate , startnavigation
public void startnavigation (view view) { intent gpsintent = new intent(selecteddock.this, gpsactivity.class); string xcorvalue = "what's wrong you?!"; gpsintent.putextra("selectedxcor", xcorvalue); startactivity(gpsintent);
in gpsactivity have:
public class gpsactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_gps); textview xcortv = (textview) findviewbyid(r.id.textview2); xcortv.settext(getintent().getextras().getstring("selectedxcor")); } }
and in app see nothing. text. if remove .settext can see default value of textview. used tutorial , simple code works, mine.. not. wrong? have add selecteddock third activity in project , first , second, used intent.
gpsintent.putextra("selectedxcor", xcorvalue); xcortv.settext(getintent().getextras().getstring("selectedxcor"));
instead of hardcoding "selectedxcor", use final static variable in different class can use variable in whole application minimizing such mistakes.
make class named "intentkeys" , declare variable
public static final string keycore = "selectedxcor";
now use wherever want them,
gpsintent.putextra(intentkeys.keycore, xcorvalue); xcortv.settext(getintent().getextras().getstring(intentkeys.keycore));
Comments
Post a Comment