java - Why do you have to declare "Integer int1" before I can use it in an onCreate and in a onClick method? -
there answer related this, didn't understand it.. i'm new related coding, go easy on me please. have:
package com.example.robert.rekenmachine; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.widget.edittext; import android.view.view; import android.widget.textview; public class mainactivity extends actionbaractivity { edittext num1text, num2text; textview ans; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); num1text = (edittext)findviewbyid(r.id.num1text); num2text = (edittext)findviewbyid(r.id.num2text); ans = (textview)findviewbyid(r.id.ans); integer int1 = integer.parseint(num1text.gettext().tostring()), int2 = integer.parseint(num2text.gettext().tostring()); float ft1 = float.parsefloat(num1text.gettext().tostring()), ft2 = float.parsefloat(num2text.gettext().tostring()); } public void add(view v){ float ansft = ft1 ans.settext(integer.tostring(1)); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
so @ float ansft = ft1, says can't resolve symbol. wondering, why? tried same things did num1text, num2text , ans, typed: float ft1;. resolve symbol. wondering if explain it, since think great deal in future if understand logic behind all.
ans
declared in scope { ... }
of class, , accessible methods field of object.
int1
local variable in 1 method. method's scope restricted braces { ... }
, variable live during call method. not accessible outside.
now 1 make int1
field. want fetch recent value of text field num1text
.
private int getint1() { int int1 = integer.parseint(num1text.gettext().tostring()); return int1; }
or simply:
private int getint1() { return integer.parseint(num1text.gettext().tostring()); }
and do
public void add(view v) { int int1 = getint1(); // take latest value. ans.settext(integer.tostring(int1));
or shorter
ans.settext(integer.tostring(getint1())); }
Comments
Post a Comment