greenfrvr / hashtag-view

Android fully customizable widget for representing data like hashtags collection and similiar.
MIT License
336 stars 54 forks source link

HashTagView calls removeAllViews in onDraw method #14

Closed arcadoss closed 8 years ago

arcadoss commented 8 years ago

HashTagView calls removeAllViews() in onDraw() method despite documentation forbid it.

    private void draw() {
        if (viewMap == null || viewMap.isEmpty()) return;
        removeAllViews();

        for (Integer key : viewMap.keySet()) {
            ViewGroup rowLayout = getRowLayout(viewMap.get(key).size());
            addView(rowLayout);
            applyDistribution(viewMap.get(key));

            for (ItemData item : viewMap.get(key)) {
                rowLayout.addView(item.view, itemLayoutParams);
            }
        }
    }

Sometime it results in crash with stacktrace like this:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
    at android.view.ViewGroup.addView(ViewGroup.java:3415)
    at android.view.ViewGroup.addView(ViewGroup.java:3391)
    at com.greenfrvr.hashtagview.HashtagView.draw(HashtagView.java:535)
    at com.greenfrvr.hashtagview.HashtagView.access$300(HashtagView.java:41)
    at com.greenfrvr.hashtagview.HashtagView$1.onPreDraw(HashtagView.java:141)
    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1870)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5603)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
    at android.view.Choreographer.doFrame(Choreographer.java:544)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5146)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
    at dalvik.system.NativeStart.main(Native Method)
greenfrvr commented 8 years ago

In fact I'm not using removeAllViews() in any of the forbidden methods, I used it inside my own method called draw() which definitely doesn't represent any step of a view or view group lifecycle.