Sygnerical
Sygnerical

Reputation: 221

Compiles And Runs But Crashes

So I'm trying to write code which will spawn a red circle at a random spot on the screen every 5 seconds. I wrote the code which will just spawn a red circle without repeating it every 5 seconds and it works fine. Then, when I added code (commented out) to draw another circle every 5 seconds, the program still compiled and ran but crashed immediately on my testing phone. Here is the code.

public class RandomCircles extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        Timer timer = new Timer();
//       timer.scheduleAtFixedRate(new TimerTask() {
//            public void run() {
                setContentView(new MyView(RandomCircles.this));
//            }
//        }, 0, 5 * 1000L);//5 seconds
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_random_circles, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

class MyView extends View {
    public MyView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        //800, 1162
        Random rand = new Random();
        x = rand.nextInt(getWidth());

        if(getWidth() - x < 100)
            x -= 100;
        else if(getWidth() - x > getWidth() - 100)
            x += 100;

        int y = rand.nextInt(getHeight());

        if(getHeight() - y < 100)
            y -= 100;
        else if(getHeight() - x > getHeight() - 100)
            y += 100;

        int radius;
        radius = 100;
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.WHITE);
        canvas.drawPaint(paint);
        // Use Color.parseColor to define HTML colors
        paint.setColor(Color.parseColor("#CD5C5C"));
        canvas.drawCircle(x, y, radius, paint);
    }
}

This is the logcat: enter image description here enter image description here

Upvotes: 0

Views: 60

Answers (1)

Arsal Imam
Arsal Imam

Reputation: 2982

Usually, any action involving the user interface must be done in the main or UI thread so, brother you can not perform any action related with the User Interface on any other thread. You can use the TimerTask in the following way,

Timer timer = new Timer();
timer.scheduleAtFixedRate( new TimerTask() {

        @Override
        public void run() 
        {
            MainActivity.this.runOnUiThread(new Runnable()
            {
                @Override
                public void run() 
                {
                    setContentView(new MyView(MainActivity.this));
                }
            });
        }
      }, 0, 5 * 1000L);

And don't forget to read this, Activity.runOnUiThread

Upvotes: 1

Related Questions