大家希望你能帮忙,谢谢你的回复 -
我有一个线程来运行硬币旋转动画,这是
public class MainActivity extends Activity {
static AnimationDrawable frameAnimation;
public boolean currentSpin = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//is this where you want me to do it
ImageView coinAnima = (ImageView) findViewById(R.id.imageView1);
Button bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
spinCoin1();
}
});
}
//end of onCreate
public void spinCoin1(){
coinAnima = (ImageView) findViewById(R.id.imageView1);
coinAnima.setBackgroundResource(R.anim.coin_spin_heads);
new Thread(new Runnable() {
public void run() {
frameAnimation = (AnimationDrawable) coinAnima.getBackground();
frameAnimation.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
frameAnimation.stop();
//end of run
}
//starts the thread
}).start();
//end of method
}
//end of class
}
它从按下按钮开始运行, 当我第二次点击按钮时会导致错误
是
03-10 23:33:28.804: E/AndroidRuntime(13689): FATAL EXCEPTION: Thread-12
03-10 23:33:28.804: E/AndroidRuntime(13689): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.view.ViewRoot.checkThread(ViewRoot.java:2936)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.view.ViewRoot.invalidateChild(ViewRoot.java:642)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:668)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.view.View.invalidate(View.java:5272)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.view.View.invalidateDrawable(View.java:7310)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.widget.ImageView.invalidateDrawable(ImageView.java:176)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.graphics.drawable.Drawable.invalidateSelf(Drawable.java:300)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.graphics.drawable.DrawableContainer.selectDrawable(DrawableContainer.java:227)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.graphics.drawable.AnimationDrawable.setFrame(AnimationDrawable.java:211)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.graphics.drawable.AnimationDrawable.nextFrame(AnimationDrawable.java:203)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.graphics.drawable.AnimationDrawable.run(AnimationDrawable.java:140)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.graphics.drawable.AnimationDrawable.start(AnimationDrawable.java:107)
03-10 23:33:28.804: E/AndroidRuntime(13689): at com.example.testanima.MainActivity$2.run(MainActivity.java:61)
03-10 23:33:28.804: E/AndroidRuntime(13689): at java.lang.Thread.run(Thread.java:1019)
任何人都可以告诉我这个错误的修复程序,再次感谢您的回复
答案 0 :(得分:1)
你不能在另一个线程中使用gui元素。这在文档中明确说明。
您可以使用以下命令更新run()中的gui元素:
public void run()
...
runOnUiThread(new Runnable() {
@Override
public void run() {
update gui elements
}
});
....
在第一个动画完成后,也可以在动画上调用clear()。然后你可以多次使用它。
答案 1 :(得分:0)
您可以在UI线程上创建coinAnima,在创建视图的其余部分时创建它,然后设置为变量。或者您可以使用使用UI线程的Handler来运行您的runnable。有关如何在UI线程http://developer.android.com/training/multiple-threads/communicate-ui.html
上设置处理程序,请参阅此链接答案 2 :(得分:0)
试试这个,
public class MainActivity extends Activity {
static AnimationDrawable frameAnimation;
public boolean currentSpin = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//is this where you want me to do it
ImageView coinAnima = (ImageView) findViewById(R.id.imageView1);
coinAnima.setBackgroundResource(R.anim.coin_spin_heads);
frameAnimation = (AnimationDrawable) coinAnima.getBackground();
Button bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
spinCoin1();
}
});
}
//end of onCreate
public void spinCoin1(){
new Thread(new Runnable() {
public void run() {
frameAnimation.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
frameAnimation.stop();
//end of run
}
//starts the thread
}).start();
//end of method
}
//end of class
}
并检查this。