Textview动画错误
Textview animation error
我正在尝试在 activity 启动时向文本视图添加动画。但是,我在 activity 启动后收到此错误(10 秒)。这是日志:
FATAL EXCEPTION: Thread-10028
Process: be.ugent.myapplication, PID: 14929
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6462)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:932)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:4693)
at android.view.View.invalidateInternal(View.java:11806)
at android.view.View.invalidate(View.java:11770)
at android.view.View.startAnimation(View.java:17877)
at be.ugent.myapplication.SplashScreen.run(SplashScreen.java:40)
这是我的 SplashScreen.Java:
public class SplashScreen extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final TextView wtext = (TextView)(findViewById(R.id.wtext));
Thread mSplashThread;
Animation animation = AnimationUtils.loadAnimation(this, R.anim.abc_fade_in);
wtext.startAnimation(animation);
// The thread to wait for 5 seconds
mSplashThread = new Thread() {
@Override
public void run(){
try {
Thread.sleep(10000);
}
catch(InterruptedException ex){
} finally{
//start animation for fadeout after 5 seconds
Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.abc_fade_out);
wtext.startAnimation(animation1);
//Start next activity
Intent intent = new Intent(SplashScreen.this,MainActivity.class);
startActivity(intent);
}
}
};
mSplashThread.start();
}
}
有人可以看一下吗?谢谢!亲切的问候!
您只能影响 UI 线程的视图。作为解决方案,您可以使用在 UI 线程中创建的处理程序。
protected void onCreate(Bundle savedInstanceState) {
Handler handler = new Handler();
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final TextView wtext = (TextView)(findViewById(R.id.wtext));
Thread mSplashThread;
Animation animation = AnimationUtils.loadAnimation(this, R.anim.abc_fade_in);
wtext.startAnimation(animation);
Runnable task = new Runnable() {
public void run() {
Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.abc_fade_out);
wtext.startAnimation(animation1);
//Start next activity
Intent intent = new Intent(SplashScreen.this,MainActivity.class);
startActivity(intent);
}
}
handler.postDelayed(task, 10000);
...
}
我正在尝试在 activity 启动时向文本视图添加动画。但是,我在 activity 启动后收到此错误(10 秒)。这是日志:
FATAL EXCEPTION: Thread-10028
Process: be.ugent.myapplication, PID: 14929
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6462)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:932)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:4693)
at android.view.View.invalidateInternal(View.java:11806)
at android.view.View.invalidate(View.java:11770)
at android.view.View.startAnimation(View.java:17877)
at be.ugent.myapplication.SplashScreen.run(SplashScreen.java:40)
这是我的 SplashScreen.Java:
public class SplashScreen extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final TextView wtext = (TextView)(findViewById(R.id.wtext));
Thread mSplashThread;
Animation animation = AnimationUtils.loadAnimation(this, R.anim.abc_fade_in);
wtext.startAnimation(animation);
// The thread to wait for 5 seconds
mSplashThread = new Thread() {
@Override
public void run(){
try {
Thread.sleep(10000);
}
catch(InterruptedException ex){
} finally{
//start animation for fadeout after 5 seconds
Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.abc_fade_out);
wtext.startAnimation(animation1);
//Start next activity
Intent intent = new Intent(SplashScreen.this,MainActivity.class);
startActivity(intent);
}
}
};
mSplashThread.start();
}
}
有人可以看一下吗?谢谢!亲切的问候!
您只能影响 UI 线程的视图。作为解决方案,您可以使用在 UI 线程中创建的处理程序。
protected void onCreate(Bundle savedInstanceState) {
Handler handler = new Handler();
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final TextView wtext = (TextView)(findViewById(R.id.wtext));
Thread mSplashThread;
Animation animation = AnimationUtils.loadAnimation(this, R.anim.abc_fade_in);
wtext.startAnimation(animation);
Runnable task = new Runnable() {
public void run() {
Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.abc_fade_out);
wtext.startAnimation(animation1);
//Start next activity
Intent intent = new Intent(SplashScreen.this,MainActivity.class);
startActivity(intent);
}
}
handler.postDelayed(task, 10000);
...
}