6

当我努力删除 RelativeLayout(动态)中的视图时,我收到一条消息错误 - 强制关闭。

添加视图:(layout是relativelayout,Sunanim的扩展View)

SunAnim rotateImg = new SunAnim(this);  
rotateImg.setTag("imgsun");
layout.addView(rotateImg);

删除视图:(CloudAnim <=> SunAnim)

class CountDownRunner implements Runnable{
        // @Override
        public void run() {
                while(!Thread.currentThread().isInterrupted()){
                    try {

                        Thread.sleep(1000);
                        delAnim ++;
                        SunAnim rotateImg = (SunAnim)layout.findViewWithTag("imgsun");
                        CloundAnim cl = (CloundAnim)layout.findViewWithTag("imgcl");
                        if (rotateImg != null && delAnim == 15) {
                            ((RelativeLayout)rotateImg.getParent()).removeView(rotateImg);   
                            layout.invalidate();
                            delAnim = 0;
                            Log.d("Rotate","Deleted");
                        }
                        if (cl != null && delAnim == 15) {
                            ((RelativeLayout)cl.getParent()).removeView(cl); 
                            layout.invalidate();
                            delAnim = 0;
                            Log.d("CL","Deleted");
                        }

                    } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                    }catch(Exception e){
                    }
                }
        }
    }

最后 - Logcat T_T :

07-06 08:14:36.326: E/AndroidRuntime(3733): Uncaught handler: thread main exiting due to uncaught exception
07-06 08:14:36.373: E/AndroidRuntime(3733): java.lang.NullPointerException
07-06 08:14:36.373: E/AndroidRuntime(3733):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1255)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at android.view.View.draw(View.java:6277)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at android.view.ViewGroup.drawChild(ViewGroup.java:1526)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at android.view.View.draw(View.java:6277)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at android.view.ViewGroup.drawChild(ViewGroup.java:1526)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at android.view.View.draw(View.java:6277)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1883)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at android.view.ViewRoot.draw(ViewRoot.java:1332)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1097)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1613)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at android.os.Looper.loop(Looper.java:123)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at android.app.ActivityThread.main(ActivityThread.java:4203)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at java.lang.reflect.Method.invokeNative(Native Method)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at java.lang.reflect.Method.invoke(Method.java:521)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
07-06 08:14:36.373: E/AndroidRuntime(3733):     at dalvik.system.NativeStart.main(Native Method)

我应该修复它吗?

4

2 回答 2

4

您正在尝试从 ui 线程以外的线程访问视图。您最好使用 Handler 实现或简单地使用

layout.post(() -> layout.invalidate());

在 ui 线程中执行视图操作。

有关更多详细信息,请参阅 https://developer.android.com/guide/components/processes-and-threads

但是,请注意,您不能从 UI 线程或“主”线程以外的任何线程更新 UI。

于 2018-09-06T11:54:10.067 回答
0

您是否尝试过在 removeView 循环中调用 layout.postInvalidate() 而不是 layout.validate()?

于 2012-07-06T02:45:41.307 回答