@tryvols
Front-End разработчик

Что именно значит ошибка?

Only the original thread that created a view hierarchy can touch its views.
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_game);

        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
        createGameField();

        findViewById(R.id.start_button).setOnClickListener(startGame);
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }
private void createGameField() {
        setGameSize();
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(brick_width, brick_height);
        int k = 0;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                brick = new Button(this);
                brick.setX(margin_left_value);
                brick.setY(margin_top_value);
                brick.setLayoutParams(layoutParams);
                brick.setOnTouchListener(this);
                game_area.addView(brick);

                bricks[k] = brick;
                k++;

                margin_left_value += brick_margin_left + brick_width;
            }
            margin_left_value = brick_margin_left;
            margin_top_value += brick_margin_top + brick_height;
        }
    }

    private void setGameSize() {
        final Point point = new Point();
        Display display = getWindowManager().getDefaultDisplay();
        display.getSize(point);

        game_area = (FrameLayout)findViewById(R.id.game_area);
        FrameLayout.LayoutParams game_area_params;
        game_area_params = (FrameLayout.LayoutParams) game_area.getLayoutParams();
        game_area_params.height = game_area_width = game_area_height = game_area_params.width = (int) point.x;

        brick_height = brick_width = game_area_width / 5;
        brick_margin_left = brick_margin_top = margin_top_value = margin_left_value =game_area_width / 25;
    }

    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {

        }
        return true;
    }

    private void hightLightBrick() {
        brick = bricks[brick_number.nextInt(15)];

brick.setBackgroundColor(Color.CYAN);  //место возникновения

}
    View.OnClickListener startGame = new View.OnClickListener() {
        public void onClick(View v) {
            timer = new Timer();
            schedule();
        }
    };

    private void schedule() {
        if (tTask != null) tTask.cancel();
        if (interval > 0) {
            tTask = new TimerTask() {
                public void run() {
                    hightLightBrick();

                    counter++;
                    if (counter == 30) {
                        counter = 0;
                        interval = (long) (interval / 1.05);
                        schedule();
                    }
                }
            };
            timer.schedule(tTask, 0, interval);
        }
    }
  • Вопрос задан
  • 327 просмотров
Решения вопроса 2
petermzg
@petermzg
Самый лучший программист
Это значит, что состояние view вы можете обновлять только из потока в котором view создан. Как правило - основного.
Передайте параметры в основной поток и из него обновите view
Ответ написан
Комментировать
Android позволяет работать с Ui только в основном потоке.Проблему можно решить классом Handler.
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
Обновлять UI вы можете воспользовавшись методом активити.
MainActivity.this.runOnUiThread(Runnable)

developer.android.com/intl/ru/reference/android/ap...
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы