java - Android开发-内部类中的变量

标签 java android

我目前正在经历的应用程序的 Android 开发(使用 Android Studio)的第二个问题。详细说明:

目标:两个箭头,一个向上,一个向下。上方和下方的框包含每次都不同的信息。

问题:在这里,我尝试根据我首先设置为 0 的变量来设置条件。如果按下向上箭头并且该变量为 0,则不执行任何操作,否则递减该变量数字。同样,如果按下向下箭头且变量为 9,则不执行任何操作,否则增加该数字。

所以这就是发生的事情。我的代码:

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);
    }
}

所以我们运行这个,我们得到这个错误:“局部变量计数器在内部类中访问;需要被声明为最终的”但是我不想要这个,因为这本质上是我 promise 我不会做任何事情这个变量。这是一个谎言,我想对这个变量做一切事情。而且 final int 也不起作用。
我无法解决这个该死的问题。好吧,那么我想,全局变量。不,也不起作用。
有人可以解释一下吗?非常感谢。

最佳答案

如错误消息所示,counteronCreate() 方法内的局部变量。一旦 onCreate() 结束,它就会消失。这就是为什么你不能从内部类引用它。

将其移至类的一个字段(也称为数据成员、成员变量),与所有其他字段一起:

public class MainActivity extends AppCompatActivity implements SensorEventListener{
  int counter=0;

  //Define the sensor Manager
  SensorManager sm;

  //Define the Motion Sensor objects
  Sensor accelerometer;
  Sensor gravity;

  // and so on

关于java - Android开发-内部类中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32854440/

相关文章:

java - LocalDateTime.format(DateTimeFormatter) 和 DateTimeFormatter.format(TemporalAccessor) 有什么区别?

java - Firebase 实时数据库 Android : How to Display Data, 一次部分

android - 是否有包含Android设备中所有崩溃报告的文件

android - 在 Web 应用程序中访问 Android 手机的 GPS,同时从该 Android 手机浏览该应用程序

java - 为类中的每个方法打开数据库连接

java - 无法使用 Selenium webdriver 写入 Excel

Android模拟器没有收到推送通知

未找到 android tensorflow ExtractImagePatches Op

java - 获取真实文件扩展名-Java代码

java - 无法使用反射调用 main 方法 - IllegalArgumentException : argument type mismatch