ened / Android-MarqueeView

Custom MarqueeView container
BSD 2-Clause "Simplified" License
121 stars 56 forks source link

Exception: The child view of this MarqueeView must be a TextView instance. #8

Open rabyunghwa opened 8 years ago

rabyunghwa commented 8 years ago

java.lang.RuntimeException: The child view of this MarqueeView must be a TextView instance.

Here is my xml file:

       <asia.ivity.android.marqueeview.MarqueeView
            android:id="@+id/marqueeView150"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            marquee:autoStart="true"
            marquee:pause="1000"
            marquee:speed="5">

            <TextView
                android:id="@+id/songname"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginStart="5dp"
                android:textColor="@android:color/white"
                android:textSize="25sp"
                android:text="Good Time"/>

        </asia.ivity.android.marqueeview.MarqueeView>
morajabi commented 8 years ago

I have the same error.

ArlonTM commented 7 years ago

I was also getting that error... Then I decompiled the class and had a quick look at the source code; something seems to happen between removing the TextView (Line 201) and replacing with the ScrollView (Line 225), and the time your onLayout gets called again.

I notice you're testing the boolean variable "changed", which is passed as a parameter to the "onLayout" method, then you proceed to do further testing on the class type of the first child in the view tree, which is supposed to be a TextView, if there is indeed a TextView inside your custom MarqueeView, as the view is supposed to be used.

However, the timing of all that seems a bit off. Your "onLayout" gets called multiple times, even after the TextView has already been replaced by a ScrollView as your logic suggests; therefore, the test on the first child in the view tree fails, because it's no longer a TextView, but rather a ScrollView this time around.

Now, which circumstances do trigger a view's "onLayout"? That's the key to the mystery. Anything that causes the view or its layout to invalidate itself and redraw, or to recalculate its dimensions, will eventually force a call on "onLayout"; that includes setting paddings, margins, width, height, relative or absolute positions, of the layout itself or any child within it. So, a change in padding in the TextView contained in your custom MarqueeView will most certainly have the effect (undesired in this circumstance) of triggering the "onLayout" procedure. But remember, not all those properties are handled at the same time, hence "onLayout" gets called after your routine has performed the replacement, thus not finding the TextView anymore.

I find, therefore, that a lot of assumptions were made, and not all angles were covered in the process of building this custom view. So, any change in the TextView's layout will eventually cause the whole structure to fall apart.

To those who are after a quick workaround to this problem, just remove any padding, margin on the TextView, leaving it to the bare required properties such as id, width, height, text, textColor, textSize, textStyle, ... I repeat, do not put anything that could possibly alter the layout! Then, to enforce the single line, in order to keep the height unchanged, add the now deprecated "singleLine" property and set it to true. Any attempt to make this a multi-line TextView will also result in the same error. Kindly check the snippet of code in the screenshot below.

snapshot

To the developer, kindly revisit your logic to improve this otherwise beautiful piece of code.

Thanks