为什么可以';我不能在Android Studio(JAVA)中的方法之外定义这些字段吗?
我开始学习如何在Android Studio中操作Java,但我仍然对一些看似基本的东西存在一些问题 这是我的密码:为什么可以';我不能在Android Studio(JAVA)中的方法之外定义这些字段吗?,java,android,field,Java,Android,Field,我开始学习如何在Android Studio中操作Java,但我仍然对一些看似基本的东西存在一些问题 这是我的密码: import android.os.Bundle; import android.view.View; import android.widget.ImageView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity {
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
public void testClick(View view) {
ImageView redTL = (ImageView) findViewById(R.id.redTL);
ImageView redT = (ImageView) findViewById(R.id.redT);
ImageView redTR = (ImageView) findViewById(R.id.redTR);
ImageView redL = (ImageView) findViewById(R.id.redL);
ImageView redC = (ImageView) findViewById(R.id.redC);
ImageView redR = (ImageView) findViewById(R.id.redR);
ImageView redBL = (ImageView) findViewById(R.id.redBL);
ImageView redB = (ImageView) findViewById(R.id.redB);
ImageView redBR = (ImageView) findViewById(R.id.redBR);
ImageView yellowTL = (ImageView) findViewById(R.id.yellowTL);
ImageView yellowT = (ImageView) findViewById(R.id.yellowT);
ImageView yellowTR = (ImageView) findViewById(R.id.yellowTR);
ImageView yellowL = (ImageView) findViewById(R.id.yellowL);
ImageView yellowC = (ImageView) findViewById(R.id.yellowC);
ImageView yellowR = (ImageView) findViewById(R.id.yellowR);
ImageView yellowBL = (ImageView) findViewById(R.id.yellowBL);
ImageView yellowB = (ImageView) findViewById(R.id.yellowB);
ImageView yellowBR = (ImageView) findViewById(R.id.yellowBR);
redTL.animate().translationYBy(1000f).setDuration(2000);
redT.animate().translationYBy(1000f).setDuration(2000);
redTR.animate().translationYBy(1000f).setDuration(2000);
redL.animate().translationYBy(1000f).setDuration(2000);
redC.animate().translationYBy(1000f).setDuration(2000);
redR.animate().translationYBy(1000f).setDuration(2000);
redBL.animate().translationYBy(1000f).setDuration(2000);
redB.animate().translationYBy(1000f).setDuration(2000);
redBR.animate().translationYBy(1000f).setDuration(2000);
yellowTL.animate().translationYBy(1000f).setDuration(2000);
yellowT.animate().translationYBy(1000f).setDuration(2000);
yellowTR.animate().translationYBy(1000f).setDuration(2000);
yellowL.animate().translationYBy(1000f).setDuration(2000);
yellowC.animate().translationYBy(1000f).setDuration(2000);
yellowR.animate().translationYBy(1000f).setDuration(2000);
yellowBL.animate().translationYBy(1000f).setDuration(2000);
yellowB.animate().translationYBy(1000f).setDuration(2000);
yellowBR.animate().translationYBy(1000f).setDuration(2000);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView redTL = (ImageView) findViewById(R.id.redTL);
ImageView redT = (ImageView) findViewById(R.id.redT);
ImageView redTR = (ImageView) findViewById(R.id.redTR);
ImageView redL = (ImageView) findViewById(R.id.redL);
ImageView redC = (ImageView) findViewById(R.id.redC);
ImageView redR = (ImageView) findViewById(R.id.redR);
ImageView redBL = (ImageView) findViewById(R.id.redBL);
ImageView redB = (ImageView) findViewById(R.id.redB);
ImageView redBR = (ImageView) findViewById(R.id.redBR);
ImageView yellowTL = (ImageView) findViewById(R.id.yellowTL);
ImageView yellowT = (ImageView) findViewById(R.id.yellowT);
ImageView yellowTR = (ImageView) findViewById(R.id.yellowTR);
ImageView yellowL = (ImageView) findViewById(R.id.yellowL);
ImageView yellowC = (ImageView) findViewById(R.id.yellowC);
ImageView yellowR = (ImageView) findViewById(R.id.yellowR);
ImageView yellowBL = (ImageView) findViewById(R.id.yellowBL);
ImageView yellowB = (ImageView) findViewById(R.id.yellowB);
ImageView yellowBR = (ImageView) findViewById(R.id.yellowBR);
redTL.setTranslationY(-1000f);
redT.setTranslationY(-1000f);
redTR.setTranslationY(-1000f);
redL.setTranslationY(-1000f);
redC.setTranslationY(-1000f);
redR.setTranslationY(-1000f);
redBL.setTranslationY(-1000f);
redB.setTranslationY(-1000f);
redBR.setTranslationY(-1000f);
yellowTL.setTranslationY(-1000f);
yellowT.setTranslationY(-1000f);
yellowTR.setTranslationY(-1000f);
yellowL.setTranslationY(-1000f);
yellowC.setTranslationY(-1000f);
yellowR.setTranslationY(-1000f);
yellowBL.setTranslationY(-1000f);
yellowB.setTranslationY(-1000f);
yellowBR.setTranslationY(-1000f);
}
}
这段代码运行得很好,但我想做的是在方法之外定义字段列表(ImageView),这样我就可以赢得一些行并使其更可见,而不是在方法onCreate和testClick中重复列表
然后我写了这个,但它不起作用:
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
ImageView redTL = (ImageView) findViewById(R.id.redTL);
ImageView redT = (ImageView) findViewById(R.id.redT);
ImageView redTR = (ImageView) findViewById(R.id.redTR);
ImageView redL = (ImageView) findViewById(R.id.redL);
ImageView redC = (ImageView) findViewById(R.id.redC);
ImageView redR = (ImageView) findViewById(R.id.redR);
ImageView redBL = (ImageView) findViewById(R.id.redBL);
ImageView redB = (ImageView) findViewById(R.id.redB);
ImageView redBR = (ImageView) findViewById(R.id.redBR);
ImageView yellowTL = (ImageView) findViewById(R.id.yellowTL);
ImageView yellowT = (ImageView) findViewById(R.id.yellowT);
ImageView yellowTR = (ImageView) findViewById(R.id.yellowTR);
ImageView yellowL = (ImageView) findViewById(R.id.yellowL);
ImageView yellowC = (ImageView) findViewById(R.id.yellowC);
ImageView yellowR = (ImageView) findViewById(R.id.yellowR);
ImageView yellowBL = (ImageView) findViewById(R.id.yellowBL);
ImageView yellowB = (ImageView) findViewById(R.id.yellowB);
ImageView yellowBR = (ImageView) findViewById(R.id.yellowBR);
public void testClick(View view) {
redTL.animate().translationYBy(1000f).setDuration(2000);
redT.animate().translationYBy(1000f).setDuration(2000);
redTR.animate().translationYBy(1000f).setDuration(2000);
redL.animate().translationYBy(1000f).setDuration(2000);
redC.animate().translationYBy(1000f).setDuration(2000);
redR.animate().translationYBy(1000f).setDuration(2000);
redBL.animate().translationYBy(1000f).setDuration(2000);
redB.animate().translationYBy(1000f).setDuration(2000);
redBR.animate().translationYBy(1000f).setDuration(2000);
yellowTL.animate().translationYBy(1000f).setDuration(2000);
yellowT.animate().translationYBy(1000f).setDuration(2000);
yellowTR.animate().translationYBy(1000f).setDuration(2000);
yellowL.animate().translationYBy(1000f).setDuration(2000);
yellowC.animate().translationYBy(1000f).setDuration(2000);
yellowR.animate().translationYBy(1000f).setDuration(2000);
yellowBL.animate().translationYBy(1000f).setDuration(2000);
yellowB.animate().translationYBy(1000f).setDuration(2000);
yellowBR.animate().translationYBy(1000f).setDuration(2000);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
redTL.setTranslationY(-1000f);
redT.setTranslationY(-1000f);
redTR.setTranslationY(-1000f);
redL.setTranslationY(-1000f);
redC.setTranslationY(-1000f);
redR.setTranslationY(-1000f);
redBL.setTranslationY(-1000f);
redB.setTranslationY(-1000f);
redBR.setTranslationY(-1000f);
yellowTL.setTranslationY(-1000f);
yellowT.setTranslationY(-1000f);
yellowTR.setTranslationY(-1000f);
yellowL.setTranslationY(-1000f);
yellowC.setTranslationY(-1000f);
yellowR.setTranslationY(-1000f);
yellowBL.setTranslationY(-1000f);
yellowB.setTranslationY(-1000f);
yellowBR.setTranslationY(-1000f);
}
}
有人能解释一下为什么它不起作用吗?我收到以下错误消息:
提前感谢您的回答任何
findViewById()
调用必须在onCreate()
中调用setContentView()
之后进行。当然,您可以使用例如ImageView-redTL代码>在任何方法之外,然后拥有redTL=(ImageView)findViewById(R.id.redTL)代码>在onCreate()
中;因此,最后,保留第一个版本更经济:/谢谢您的回答