循环使用onCreate方法 [英] Looping in onCreate method

查看:165
本文介绍了循环使用onCreate方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在创建一个程序,该程序每5秒随机在屏幕上生成一个圆圈.我只显示1个圆就使它起作用,但是当我尝试在onCreate方法中循环绘制多个圆时,出现编译器错误,我不知道现在该怎么办,被卡了30分钟.这是我的代码和主要错误.任何帮助将不胜感激.

So I'm creating a program which will randomly spawn circles on the screen every 5 seconds. I had it working by just displaying 1 circle but when I tried to loop in the onCreate method to draw multiple circles, I got compiler errors and I have no idea what to do now, been stuck for 30 minutes. Here is my code and the main error. Any help will be appreciated.

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(R.layout.activity_random_circles);
            }
        }, 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();

        double x = rand.nextInt(getWidth());

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

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

推荐答案

new MyView(Context context)

MyView构造函数需要一个上下文".您正在传递布局ID(它是整数值). 应该是

MyView constructor needs a "context". You are passing a layout id (it's integer value). It should be

new MyView(this)

这篇关于循环使用onCreate方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆