Я делаю приложение для Android, которое позволяет пользователям нажимать кнопку «плюс» или «минус», чтобы изменить номер, отображаемый в текстовом режиме.Android: настройка ID для массива кнопок изображения
Есть 18 текстовых просмотров и кнопка плюс и минус изображения для каждого текстового вида.
Активность падает, когда я добираюсь до этого действия, из-за этого раздела кода. Я не смог получить какую-либо полезную информацию при отладке кода, поэтому любая помощь будет оценена, спасибо!
В верхней части моей деятельности:
int score = 0;
ImageButton addButtons[] = {(ImageButton)findViewById(R.id.hole1Up), (ImageButton)findViewById(R.id.hole2Up), (ImageButton)findViewById(R.id.hole3Up), (ImageButton)findViewById(R.id.hole4Up), (ImageButton)findViewById(R.id.hole5Up), (ImageButton)findViewById(R.id.hole6Up), (ImageButton)findViewById(R.id.hole7Up), (ImageButton)findViewById(R.id.hole8Up), (ImageButton)findViewById(R.id.hole9Up), (ImageButton)findViewById(R.id.hole10Up), (ImageButton)findViewById(R.id.hole11Up), (ImageButton)findViewById(R.id.hole12Up), (ImageButton)findViewById(R.id.hole13Up), (ImageButton)findViewById(R.id.hole14Up), (ImageButton)findViewById(R.id.hole15Up), (ImageButton)findViewById(R.id.hole16Up), (ImageButton)findViewById(R.id.hole17Up), (ImageButton)findViewById(R.id.hole18Up),};
ImageButton minusButtons[] = {(ImageButton)findViewById(R.id.hole1Down), (ImageButton)findViewById(R.id.hole2Down), (ImageButton)findViewById(R.id.hole3Down), (ImageButton)findViewById(R.id.hole4Down), (ImageButton)findViewById(R.id.hole5Down), (ImageButton)findViewById(R.id.hole6Down), (ImageButton)findViewById(R.id.hole7Down), (ImageButton)findViewById(R.id.hole8Down), (ImageButton)findViewById(R.id.hole9Down), (ImageButton)findViewById(R.id.hole10Down), (ImageButton)findViewById(R.id.hole11Down), (ImageButton)findViewById(R.id.hole12Down), (ImageButton)findViewById(R.id.hole13Down), (ImageButton)findViewById(R.id.hole14Down), (ImageButton)findViewById(R.id.hole15Down), (ImageButton)findViewById(R.id.hole16Down), (ImageButton)findViewById(R.id.hole17Down), (ImageButton)findViewById(R.id.hole18Down),};
int[] scoreIDs = new int[] {R.id.score1, R.id.score2, R.id.score3, R.id.score4, R.id.score5, R.id.score6, R.id.score7, R.id.score8, R.id.score9, R.id.score10, R.id.score11, R.id.score12, R.id.score13, R.id.score14, R.id.score15, R.id.score16, R.id.score17, R.id.score18,};
public void setPlusButtons()
{
for(int i = 0; i < addButtons.length; i++)
{
final int j = i;
ImageButton button = addButtons[i];
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView tv5 = (TextView) findViewById(scoreIDs[j]);
tv5.setText(score++);
}
});
}
}
public void setMinusButtons()
{
for(int i = 0; i < minusButtons.length; i++)
{
final int j = i;
ImageButton button = minusButtons[i];
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView tv5 = (TextView) findViewById(scoreIDs[j]);
tv5.setText(score--);
}
});
}
}
Мой OnCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course);
Bundle extras = getIntent().getExtras();
if (extras != null) {
chosenCourseValue = extras.getString("passedChosenCourse");
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
AsyncAPI APIThread = new AsyncAPI();
APIThread.execute();
setPlusButtons();
setMinusButtons();
}
раздел моей деятельности XML
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Holes"
android:id="@+id/hole1"
android:layout_marginLeft="32dp"
android:textColor="#fff"
android:gravity="center"
android:background="@drawable/circle"
android:height="45dp"
android:width="45dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pars"
android:id="@+id/hole1Par"
android:paddingLeft="22dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score"
android:id="@+id/score1"
android:paddingLeft="42dp" />
<ImageButton
android:layout_width="30dp"
android:layout_height="30dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:id="@+id/hole1Up"
android:gravity="center"
android:layout_marginLeft="32dp"
android:layout_marginTop="6dp"
android:src="@drawable/plus"
android:background="@null" />
<ImageButton
android:layout_width="32dp"
android:layout_height="32dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:id="@+id/hole1Down"
android:gravity="center"
android:layout_marginLeft="26dp"
android:layout_marginTop="6dp"
android:src="@drawable/minus"
android:background="@null" />
</LinearLayout>
Мне не удалось получить сообщение об ошибке путем отладки, так что надеялся, что кто-то сможет указать, что я делаю неправильно с моим кодом, см. Пункт 3. – OJJ
Эй, OJJ, я думаю, вместо этого вы можете использовать «ListView или RecylerView» с компоновкой вырезов и адаптером. – theapache64
Можете ли вы опубликовать файл макета 'activity_course' xml? У меня есть основная идея сократить код и сложность кода. – user370305