My second question for android development (using android studio) of the app I'm currently suffering through. To elaborate:
The Aim: Two arrows, one up, one down. Above and below a box that contains information that is each time different.
The Problem: Here I try to set up conditionals based off a variable that I set first to be 0. If the up arrow is pressed and that variable is 0, do nothing, otherwise decrement that number. Likewise, if the down arrow is press and the variable is 9 do nothing, otherwise increment that number.
So this is what happens. My 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 the sensor Manager
SensorManager sm;
//Define the Motion Sensor objects
Sensor accelerometer;
Sensor gravity;
Sensor gyroscope;
//Sensor uncalgyro;
Sensor lineaccel;
Sensor rotatevector;
//Sensor sigmotion;
//Sensor stepcounter;
//Sensor stepdetector;
//Define the changing Motion Sensor text values
TextView gyrosense;
//TextView uncalgyrosense;
TextView acceleration;
TextView gravitysense;
TextView lineaccelsense;
TextView rotatesense;
//TextView sigmotionsense; //In order to make use of this, use onTrigger event
//TextView stepstaken;
//TextView stepdetected; //In order to make use of this, use onTrigger event
//Define the position sensor objects
Sensor gamerotatevector;
//Define the 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 for now
arrowup.setVisibility(View.GONE);
arrowdown.setVisibility(View.GONE);
navbar.setVisibility(View.GONE);
button1.setOnClickListener( //This is for the 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 the other two buttons are normal again
button2.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.positionbutton_normal, null));
button3.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.envirobutton_normal, null));
//make the arrows and infospace appear
arrowup.setVisibility(View.VISIBLE);
arrowdown.setVisibility(View.VISIBLE);
navbar.setVisibility(View.VISIBLE);
}
}
);
button2.setOnClickListener( //This is for the 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 the other two buttons are 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 is for the 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 the other two buttons are 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 is for the 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 the menu; this adds items to the action bar if it is 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. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
So we run this and we get this error: "local variable counter is accessed within inner class; needs to be declared final" However I DO NOT want this, as this is essentially me promising I'm going to do nothing with this variable. Which is a lie, I want to do everything to this variable. Also final int doesn't bloody work either.
I can't solve the damned problem. Okay so then I think, global variable then. Nope, doesn't work either.
can someone please shed some light. Many thanks in advance.
int countervariable as a member variable (a.k.a global variable, a.k.a field)private int counter = 0;.