I get an message error - force close - when I effort remove view in RelativeLayout (dynamic).
add view : (layout is relativelayout, Sunanim's extended View)
SunAnim rotateImg = new SunAnim(this);
rotateImg.setTag("imgsun");
layout.addView(rotateImg);
remove view :(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){
}
}
}
}
and finally - 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)
Should i do fix it ?
You are trying to access a view from a thread other then ui thread. You better use a Handler implementation or simply use
layout.post(() -> layout.invalidate());
to perform view operation in ui thread.
For more details see, https://developer.android.com/guide/components/processes-and-threads
However, note that you cannot update the UI from any thread other than the UI thread or the "main" thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With