Я начинаю учиться манипулировать Java в Android Studio, но у меня все еще есть некоторые проблемы с тем, что кажется базовым c вещами ..
Вот мой код:
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);
}
}
Этот код работает хорошо, но я хотел бы определить список полей (ImageViews) вне методов, чтобы я мог выиграть некоторые строки и сделать его более видимым вместо повторения списка в методе 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);
}
}
Может кто-нибудь объяснить, почему это не работает? Я получаю это сообщение об ошибке: К сожалению, «Имя моего проекта» остановлено
Заранее благодарю за ответы