内部类中的java Android开发变量
我的第二个问题是关于我目前正在经历的应用程序的安卓开发(使用安卓 studio)。详细说明:
瞄准:两个箭头,一个向上,一个向下。上方和下方一个框,其中包含每次都不同的信息
问题:在这里,我尝试根据我首先设置为0的变量设置条件。如果按下向上箭头且该变量为0,则不执行任何操作,否则减小该数字。同样,如果按下向下箭头且变量为9,则不执行任何操作,否则增加该数字
这就是发生的事情。我的代码:
package com.example.savag.myapplication;
import 安卓.hardware.SensorEvent;
import 安卓.hardware.SensorEventListener;
import 安卓.media.Image;
import 安卓.provider.Settings;
import 安卓.support.v4.content.res.ResourcesCompat;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.widget.ImageButton;
import 安卓.widget.ImageView;
import 安卓.widget.LinearLayout;
import 安卓.hardware.Sensor;
import 安卓.hardware.SensorManager;
import 安卓.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
也不起作用
我解决不了这个该死的问题。好吧,那么我想,全局变量。不,也不行
谁能帮我弄点光吗。非常感谢
# 1 楼答案
将此代码(内部类)放入活动类中:
# 2 楼答案
如错误消息所示,
counter
是onCreate()
方法内部的局部变量。它一结束就消失了。这就是为什么不能从内部类引用它将其移动到类的字段(也称为数据成员、成员变量)以及所有其他字段:
# 3 楼答案
您拥有的代码不是正确的代码。我想它一定在oncreate中。如果是这样,它只是一个普通的stackvariable,没有全局对象
像我说的那样编辑,现在已经看到了您的全部代码:变量计数器必须是全局的,而不是onCreate()中的局部
这就是为什么AS想告诉你它需要一个最终的变量
引用Java官方文档:
此外,本地类可以访问本地变量。但是,本地类只能访问声明为final的本地变量
您可以看到计数器是局部变量,而不是全局变量