Android开发 - 内部类

时间:2015-09-29 21:57:43

标签: 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);
    }
}

所以我们运行这个并且我们得到这个错误:“在内部类中访问局部变量计数器;需要被声明为final”但是我不想要这个,因为这基本上是我承诺我将不会做任何事情这个变量。这是谎言,我想对这个变量做一切。 final int也没有血腥的工作 我无法解决该死的问题。那么我认为,全球变量那么。不,也不起作用。
有人可以请一些亮点。非常感谢提前。

3 个答案:

答案 0 :(得分:1)

将此代码(内部类)放在您的活动类中:

private class MyImageButton  extends ImageButton{

private int myCounter=0;
private String myStrCounter=Integer.toString(myCounter);
public MyImageButton(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}




    arrowup.setOnClickListener( //This is for the environment button
            new MyImageButton.OnClickListener(){
                public void onClick(View v){
                    if (myStrCounter.equals("0"))
                    {
                    }
                    else
                    {
                        myCounter--; //how many times
                        myStrCounter = Integer.toString(myCounter);

                        //now lets set text accordingly
                        if (myStrCounter.equals("1")) {

                        }

                    }

                    }
                });

答案 1 :(得分:1)

您拥有的此代码不是正确的代码。我认为它必须在oncreate内。如果是这样,它只是一个普通的stackvariable而没有全局对象。

编辑就像我说的那样,现在已经看到了你的整个代码:变量计数器必须是全局的而不是本地的onCreate()

这就是为什么AS想要告诉你它需要一个最终的变量。

来自官方Java Docs:

"此外,本地类可以访问本地变量。但是,本地类只能访问声明为final的局部变量。"

您看到您的计数器是局部变量而非全局变量

答案 2 :(得分:1)

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

将其移动为该类的字段(a.k.a.,数据成员,成员变量)以及所有其他字段:

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