在onCreate方法中循环

时间:2015-06-15 03:01:02

标签: android oncreate

所以我创建了一个程序,它会每隔5秒在屏幕上随机产生圆圈。我只是显示1个圆圈,但是当我试图在onCreate方法中循环绘制多个圆圈时,我遇到编译错误,我不知道现在该做什么,被卡住了30分钟。这是我的代码和主要错误。任何帮助将不胜感激。

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

enter image description here

3 个答案:

答案 0 :(得分:1)

您的render nothing: true 构造函数将MyView作为参数,但您传递Context,这基本上是映射到R.layout.activity_random_circles中的布局xml文件的自动生成的int值。尝试传递res/layout而不是引用您的RandomCircles.this上下文

答案 1 :(得分:1)

MyView构造函数需要Context参数,但您传递的是布局ID。您的MyView构造函数应该看起来像这样

public class MyView extends View {

public MyView(Context context) {
    super(context);

    View view = inflate(context, R.layout.activity_random_circles, null);
    view.setFocusable(true);
    addView(view);
}
..
}

然后在你的onCreate()方法中,执行此操作

public void run() {
    setContentView(new MyView(this));
}

答案 2 :(得分:1)

&#13;
&#13;
new MyView(Context context)
&#13;
&#13;
&#13;

MyView构造函数需要&#34; context&#34;。您正在传递布局ID(它的整数值)。 它应该是

&#13;
&#13;
new MyView(this)
&#13;
&#13;
&#13;