【问题标题】:Textview animation errorTextview动画错误
【发布时间】:2015-11-23 20:29:45
【问题描述】:

我正在尝试在我的活动开始时向文本视图添加动画。但是,我在活动开始后收到此错误(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$1.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();
    }
}

有人可以看看吗?谢谢!亲切的问候!

【问题讨论】:

    标签: android animation textview splash-screen


    【解决方案1】:

    您只能从 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);
        ...
    }
    

    【讨论】:

    • 现在如果我不想开始另一个活动,所以删除 Intent,我怎样才能让我的屏幕保持空白(所以没有我的文字)?它不断重新出现......
    猜你喜欢
    • 2014-01-31
    • 2013-08-14
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 2012-02-22
    相关资源
    最近更新 更多