質問
こんにちは 私はいくつかの形状は、すべてが開かれますレイアウトを構築しようとしている2 秒。ユーザーはこれらの形状のいずれかをクリックする場合は、彼らがする必要はあり 消えます。
これを行うための正しい方法は何ですか?私は、スレッドについて考えたが、私 逃しました。 ここでは現時点では私のコードです(動作しない):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
l = new LinearLayout(this);
setContentView(l);
int counter = 1;
View v = new CustomDrawableView(this,20,50);
l.addView(v);
Thread t = new Thread() {
public void run() {
while (true) {
Log.i("THREAD","INSIDE");
View h = new CustomDrawableView(c,
(int)Math.round(Math.random()*100),
(int)Math.round(Math.random()*100));
SystemClock.sleep(2000);
l.addView(h);
}
}
};
t.start();
}
解決
あなたは別のスレッドで画面を操作することはできません。それは、UIスレッドで呼び出されるので、あなたはハンドラを使用する必要があります。
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
l = new LinearLayout(this);
setContentView(l);
int counter = 1;
View v = new CustomDrawableView(this,20,50);
l.addView(v);
ShapeHandler handler = new ShapeHandler();
handler.sendEmptyMessage(0);
}
private class ShapeHandler extends Handler
{
@Override
public void handleMessage(Message msg)
{
View h = new CustomDrawableView(c,
(int)Math.round(Math.random()*100),
(int)Math.round(Math.random()*100));
l.addView(h);
this.sendEmptyMessageDelayed(0, 2000);
}
}
所属していません StackOverflow