由于线程使用我的应用程序崩溃我该如何解决它

时间:2014-05-27 04:43:17

标签: android multithreading

我正在使用线程onClick方法。它运行成功,但在崩溃后。我不知道为什么......把代码放在这里

@Override
        public void onClick(View v) {
            // invisible first image
            // imgVScrub.setVisibility(View.INVISIBLE);
            imgMoveCream.setVisibility(View.VISIBLE);
            // translate the particle by anim

            forCreamAnim.start();
            // imgVTemp.startAnimation(creamParticleAnim);
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                    } catch (Exception e) {
                        // : handle exception
                        e.printStackTrace();
                    } finally {
                        // back animation of cream
                        imgVScrub.startAnimation(showerBackRotateAnimCream);

                    }
                }
            }).start();
        }

并且日志跟踪在这里

05-27 04:30:29.732: E/AndroidRuntime(1682): FATAL EXCEPTION: Thread-90
05-27 04:30:29.732: E/AndroidRuntime(1682):   android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
05-27 04:30:29.732: E/AndroidRuntime(1682):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4039)
05-27 04:30:29.732: E/AndroidRuntime(1682):     at android.view.ViewRootImpl.invalidateChild(ViewRootImpl.java:722)
05-27 04:30:29.732: E/AndroidRuntime(1682):     at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:771)
05-27 04:30:29.732: E/AndroidRuntime(1682):     at android.view.ViewGroup.invalidateChild(ViewGroup.java:4005)
05-27 04:30:29.732: E/AndroidRuntime(1682):     at android.view.View.invalidate(View.java:8576)
05-27 04:30:29.732: E/AndroidRuntime(1682):     at android.view.View.startAnimation(View.java:12980)
05-27 04:30:29.732: E/AndroidRuntime(1682):     at dwt.wasif.manicure.CreamActivity$1$1.run(CreamActivity.java:102)
05-27 04:30:29.732: E/AndroidRuntime(1682):     at java.lang.Thread.run(Thread.java:856)

4 个答案:

答案 0 :(得分:1)

试试这个。用这段代码替换你的finally块

finally { 

YourActivity.this.runOnUiThread(new Runnable() {

        @Override 
        public void run() { 

            // back animation of cream 
             imgVScrub.startAnimation(showerBackRotateAnimCream); 

        } 
    }); 

}

答案 1 :(得分:1)

只需在ui thread中启动你的动画..所有ui相关的更改都需要在ui线程中完成

使用下面的代码

ActiviyName.this.runOnUiThread(new Runnable() {

    public void run() { 

        // back animation of cream 
         imgVScrub.startAnimation(showerBackRotateAnimCream); 

    } 
}); 

答案 2 :(得分:0)

您无法在后台线程中调用startAnimation()。

Android只允许你在... UI线程上做UI事情。您当前正在后台线程中的Runnable中调用imgVScrub.startAnimation(showerBackRotateAnimCream);,这会引发此错误。

只需将动画开始移动到正常的UI线程代码中即可。

答案 3 :(得分:0)

在主线程中移动imgVScrub.startAnimation(showerBackRotateAnimCream);

如果你想在后台执行与Ui相关的任务,你必须调用runOnUiThread(),这会使事情变得复杂。所以在主线程中调用imgVScrub.startAnimation(showerBackRotateAnimCream);