java - Android development - variable inside inner class -
my second question android development (using android studio) of app i'm suffering through. elaborate:
the aim: 2 arrows, 1 up, 1 down. above , below box contains information each time different.
the problem: here try set conditionals based off variable set first 0. if arrow pressed , variable 0, nothing, otherwise decrement number. likewise, if down arrow press , variable 9 nothing, otherwise increment number.
so happens. code:
package com.example.savag.myapplication; import android.hardware.sensorevent; import android.hardware.sensoreventlistener; import android.media.image; import android.provider.settings; import android.support.v4.content.res.resourcescompat; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.imagebutton; import android.widget.imageview; import android.widget.linearlayout; import android.hardware.sensor; import android.hardware.sensormanager; import android.widget.textview; public class mainactivity extends appcompatactivity implements sensoreventlistener{ //define sensor manager sensormanager sm; //define motion sensor objects sensor accelerometer; sensor gravity; sensor gyroscope; //sensor uncalgyro; sensor lineaccel; sensor rotatevector; //sensor sigmotion; //sensor stepcounter; //sensor stepdetector; //define changing motion sensor text values textview gyrosense; //textview uncalgyrosense; textview acceleration; textview gravitysense; textview lineaccelsense; textview rotatesense; //textview sigmotionsense; //in order make use of this, use ontrigger event //textview stepstaken; //textview stepdetected; //in order make use of this, use ontrigger event //define position sensor objects sensor gamerotatevector; //define changing position sensor objects textview gamerotatesense; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //variable declare final imagebutton button1 = (imagebutton)findviewbyid(r.id.motbut_unpressed); final imageview topbar2 = (imageview)findviewbyid(r.id.topbarmain); final imagebutton button2 = (imagebutton)findviewbyid(r.id.posbut_unpressed); final imagebutton button3 = (imagebutton)findviewbyid(r.id.envbut_unpressed); final linearlayout andfield = (linearlayout)findviewbyid(r.id.babcocklayout); final imagebutton arrowup = (imagebutton)findviewbyid(r.id.uparrow); final imagebutton arrowdown = (imagebutton)findviewbyid(r.id.downarrow); final imageview navbar = (imageview)findviewbyid(r.id.infospace); int counter; counter = 0; final string strcounter; strcounter = integer.tostring(counter); //set nav bar invisible arrowup.setvisibility(view.gone); arrowdown.setvisibility(view.gone); navbar.setvisibility(view.gone); button1.setonclicklistener( //this motion button new imagebutton.onclicklistener() { public void onclick(view v) { button1.setbackground(resourcescompat.getdrawable(getresources(), r.drawable.motionbutton_pressed, null)); topbar2.setbackground(resourcescompat.getdrawable(getresources(), r.drawable.topbarmotion, null)); ndfield.setbackground(resourcescompat.getdrawable(getresources(), r.drawable.logoandbarmotion, null)); //make sure other 2 buttons normal again button2.setbackground(resourcescompat.getdrawable(getresources(), r.drawable.positionbutton_normal, null)); button3.setbackground(resourcescompat.getdrawable(getresources(), r.drawable.envirobutton_normal, null)); //make arrows , infospace appear arrowup.setvisibility(view.visible); arrowdown.setvisibility(view.visible); navbar.setvisibility(view.visible); } } ); button2.setonclicklistener( //this position button new imagebutton.onclicklistener(){ public void onclick(view v){ button2.setbackground(resourcescompat.getdrawable(getresources(), r.drawable.positionbutton_pressed, null)); topbar2.setbackground(resourcescompat.getdrawable(getresources(), r.drawable.topbarposition, null)); andfield.setbackground(resourcescompat.getdrawable(getresources(), r.drawable.logoandbaralternate, null)); //make sure other 2 buttons normal again button1.setbackground(resourcescompat.getdrawable(getresources(), r.drawable.motionbutton_normal, null)); button3.setbackground(resourcescompat.getdrawable(getresources(), r.drawable.envirobutton_normal, null)); arrowup.setvisibility(view.visible); arrowdown.setvisibility(view.visible); navbar.setvisibility(view.visible); } } ); button3.setonclicklistener( //this environment button new imagebutton.onclicklistener(){ public void onclick(view v){ button3.setbackground(resourcescompat.getdrawable(getresources(), r.drawable.envirobutton_pressed, null)); topbar2.setbackground(resourcescompat.getdrawable(getresources(), r.drawable.topbarenviro, null)); andfield.setbackground(resourcescompat.getdrawable(getresources(), r.drawable.logoandbaralternate, null)); //make sure other 2 buttons normal again button1.setbackground(resourcescompat.getdrawable(getresources(), r.drawable.motionbutton_normal, null)); button2.setbackground(resourcescompat.getdrawable(getresources(), r.drawable.positionbutton_normal, null)); arrowup.setvisibility(view.visible); arrowdown.setvisibility(view.visible); navbar.setvisibility(view.visible); } } ); arrowup.setonclicklistener( //this environment button new imagebutton.onclicklistener(){ public void onclick(view v){ if (strcounter.equals("0")) { } else { counter--; //how many times strcounter = integer.tostring(counter); //et1.settext(strcounter); //now lets set text accordingly if (strcounter.equals("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 void onaccuracychanged(sensor sensor, int accuracy) { } @override public void onsensorchanged(sensorevent event) { } @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 run , error: "local variable counter accessed within inner class; needs declared final" not want this, me promising i'm going nothing variable. lie, want variable. final int
doesn't bloody work either.
can't solve damned problem. okay think, global variable then. nope, doesn't work either.
can please shed light. many in advance.
as error message indicates, counter
local variable inside of oncreate()
method. goes away oncreate()
ends. why cannot reference inner classes.
move field (a.k.a., data member, member variable) of class, alongside other fields:
public class mainactivity extends appcompatactivity implements sensoreventlistener{ int counter=0; //define sensor manager sensormanager sm; //define motion sensor objects sensor accelerometer; sensor gravity; // , on
Comments
Post a Comment