java - Accessing a variable from one class that is located in another class -
iv been stuck on bug, in android app, 3 weeks now. killing me... :(. can please fix code me (its small error), , can learn it. basically, have mainactivity class holds variable "dblcountvalue". want access variable in withdraw class. here code:
class: mainactivity
package com.mycash.borgjake.mycash; import android.content.intent; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.widget.button; import android.widget.textview; import android.view.view; import com.google.android.gms.ads.adlistener; import com.google.android.gms.ads.adrequest; import com.google.android.gms.ads.adview; import com.google.android.gms.ads.interstitialad; import static com.mycash.borgjake.mycash.r.styleable.view; public class mainactivity extends appcompatactivity { private interstitialad minterstitial; button btnclick; button btnwithdraw; textview txtbalance; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); adview adview = (adview)findviewbyid(r.id.adview); adrequest adrequest = new adrequest.builder() .addtestdevice(adrequest.device_id_emulator) .build(); adview.loadad(adrequest); btnclick = (button) findviewbyid(r.id.button); txtbalance = (textview) findviewbyid(r.id.textview); btnclick.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v){ string countvalue = txtbalance.gettext().tostring(); int dblcountvalue = integer.parseint(countvalue); if (minterstitial.isloaded()) { minterstitial.show(); } //minterstitial.loadad(request); dblcountvalue++; txtbalance.settext(string.valueof(dblcountvalue)); } }); minterstitial = new interstitialad(this); minterstitial.setadunitid("..."); adrequest request = new adrequest.builder() .addtestdevice(adrequest.device_id_emulator) .build(); minterstitial.loadad(request); } public void onbuttonclick(view v) { if(v.getid() == r.id.button2) { intent = new intent(mainactivity.this, withdraw.class); startactivity(i); } } }
class: withdraw
package com.mycash.borgjake.mycash; import android.app.activity; import android.app.alertdialog; import android.os.bundle; import android.view.view; import com.google.android.gms.ads.adrequest; import com.google.android.gms.ads.adview; public class withdraw extends activity { mainactivity mainactivityobject = new mainactivity(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.withdraw); adview adview = (adview) findviewbyid(r.id.adview2); adrequest adrequest = new adrequest.builder() .addtestdevice(adrequest.device_id_emulator) .build(); adview.loadad(adrequest); } public void showalert(view view) { alertdialog.builder myalert = new alertdialog.builder(this); string messagewithdraw; string awithdraw; string dwithdraw; awithdraw = "congradulations! payment being processed"; dwithdraw = "sorry! please try again when reach 100 points ($10)"; dblcountvalue = mainactivityobject.dblcountvalue; // <-- error if (dblcountvalue > 100) { // <-- error myalert.setmessage(awithdraw) .create(); myalert.show(); } else { myalert.setmessage(dwithdraw) .create(); myalert.show(); } } }
thanks lot in advance :)
to send variable 1 activity other activity should use intent object.in case have create intent object in mainactivity , intent object can send data withdraw class.
you should write code in mainactivity.
public void onclick(view v){ string countvalue = txtbalance.gettext().tostring(); int dblcountvalue = integer.parseint(countvalue); if (minterstitial.isloaded()) { minterstitial.show(); } //minterstitial.loadad(request); dblcountvalue++; txtbalance.settext(string.valueof(dblcountvalue)); intent intent = new intent(mainactivity.this, withdraw.class); intent.putextra( "key" , dblcountvalue); startactivity(intent); } });
now section of code take place in withdraw class in side oncreate method .here extract dblcountvalue intet object using key .
in withdraw class replace dblcountvalue = mainactivityobject.dblcountvalue;
with following code section
bundle bundle = getintent().getextras(); int dblcountvalue =bundle.getint("key",0);
Comments
Post a Comment