2012-07-06 63 views
5

我收到消息錯誤 - 強制關閉 - 當我努力刪除RelativeLayout(動態)中的視圖。Android - 如何刪除在佈局中查看(動態添加視圖)

添加視圖:(佈局的RelativeLayout,Sunanim的擴展視圖)

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) 

我應該修復它嗎?

+0

沒人能幫幫我嗎? – user1465351 2012-07-06 11:40:15

回答

0

您是否嘗試過在removeView循環中調用layout.postInvalidate()而不是layout.validate()?

+0

我嘗試了它,但它不起作用 – user1465351 2012-07-06 11:17:16

+0

你是否調用了ui線程中的remove view thread? – frogmanx 2012-07-07 01:08:39

相關問題