Почему вылетает приложение при запуске 34 строки?
При запуске андроид-приложения, приложение вылетает, заметил, что вылетает при выполнении 34 строки.
package com.example.svetovor;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
private LinearLayout first_square, second_square, third_square;
private Button button_first;
private boolean start_stop = false;
private int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
second_square = findViewById(R.id.cecond_square);
third_square = findViewById(R.id.third_square);
button_first = findViewById(R.id.button_first);
}
public void onClickStart(View view){
if(!start_stop) {button_first.setText("Stop");
start_stop = true;
new Thread(new Runnable() {
@Override
public void run() {
while (start_stop) {
counter++;
if(counter == 1) {
first_square.setBackgroundColor(getResources().getColor(R.color.gray));
first_square.setBackgroundColor(getResources().getColor(R.color.red));
} else if (counter == 2) {
second_square.setBackgroundColor(getResources().getColor(R.color.gray));
second_square.setBackgroundColor(getResources().getColor(R.color.yellow));
} else if (counter == 3) {
counter = 0;
third_square.setBackgroundColor(getResources().getColor(R.color.gray));
third_square.setBackgroundColor(getResources().getColor(R.color.green));
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}).start();
} else {
start_stop = false;
button_first.setText("Start");
}
}
@Override
protected void onDestroy() {
super.onDestroy();
start_stop = false;
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/first_square"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="150dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="150dp"
android:layout_marginBottom="600dp"
android:background="@color/gray"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></LinearLayout>
<LinearLayout
android:id="@+id/third_square"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="150dp"
android:layout_marginEnd="150dp"
android:layout_marginBottom="363dp"
android:orientation="horizontal"
android:background="@color/gray"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.545"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cecond_square"
app:layout_constraintVertical_bias="1.0">
</LinearLayout>
<LinearLayout
android:id="@+id/cecond_square"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="150dp"
android:layout_marginTop="150dp"
android:layout_marginEnd="150dp"
android:layout_marginBottom="480dp"
android:orientation="horizontal"
android:background="@color/gray"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" >
</LinearLayout>
<Button
android:id="@+id/button_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="150dp"
android:layout_marginTop="76dp"
android:layout_marginEnd="150dp"
android:layout_weight="1"
android:onClick="onClickStart"
android:text="@string/button_1"
app:cornerRadius="8sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.478"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/third_square"
app:strokeColor="#000"
app:strokeWidth=
"3sp" />
Ответы (1 шт):
Автор решения: Котлетка еее
→ Ссылка
Вы пытаетесь повлиять на компоненты интерфейса приложения из другого потока. Чтобы ошибок не возникало, можно использовать метод post() какого-нибудь графического компонента View. Например:
first_sqaure.post(() -> {
first_square.setBackgroundColor(getResources().getColor(R.color.gray));
first_square.setBackgroundColor(getResources().getColor(R.color.red));
});
И с остальными тоже так.