N
N
Nikolay2016-01-05 01:12:04
Java
Nikolay, 2016-01-05 01:12:04

What exactly does the error mean?

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);
        }
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Peter, 2016-01-05
@tryvols

This means that you can only update the state of a view from the thread in which the view was created. Usually the main one.
Pass parameters to the main thread and update the view from it

R
Roman Artemov, 2016-01-12
@roman_art

You can update the UI using the activation method.
MainActivity.this.runOnUiThread(Runnable)
developer.android.com/intl/en/reference/android/ap...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question