Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextClock render problem in layout preview

When i created TextClock widget in layout Android Studio layout preview cahsed and didn't show the changes. But on debug devise widget work perfectly. If I commented it layout preview work successfuly. Another PC Android Studio work successfuly and show TextClock.

Error Layout preview

java.lang.NullPointerException
at android.content.ContentResolver.registerContentObserver(ContentResolver.java:2263)
at android.widget.TextClock.registerObserver(TextClock.java:626)
at android.widget.TextClock.onAttachedToWindow(TextClock.java:545)
at android.view.View.dispatchAttachedToWindow(View.java:19575)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
at android.view.AttachInfo_Accessor.setAttachInfo(AttachInfo_Accessor.java:44)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:355)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:404)
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:657)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$7(RenderTask.java:788)
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

TextClock XML code

<TextClock
            android:layout_width="120dp"
            android:layout_height="35dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginEnd="16dp"
            android:format24Hour="dd.MMMyyyy, HH:mm"
            android:format12Hour="dd.MMM.yyyy, hh:mm a"
            android:textColor="#d0ffffff"
            android:textSize="30sp" />

Changing minSdkVersion to 17 and creating layout v17 didn't give any results. Problem found in Android Studio 3.6, 3.6.1 and 4.0

like image 746
Typikal_Failman Avatar asked Mar 06 '20 13:03

Typikal_Failman


People also ask

Why does the layout preview window not render chip elements?

After upgrading to version 1.2.0-alpha03 the layout preview window does not render Chip elements. They are drawn completely blank without any background, stroke, text or icons. This is solely a visual issue with the preview window as Chips are properly rendered when running on a device.

How to create textclock in XML layout file in Android?

In android, we can create TextClock in XML layout file using <TextClock> element with different attributes like as shown below. If you observe above code snippet, we used android:format12Hour attribute to set the 12 Hours time format based on our requirements.

What is textclock in Android?

In android, TextClock is a UI control that is used to show the current date or time as a formatted string. The TextClock provides a date/time in two formats, one is to show the date/time in 24 Hours format and another one is to show the date / time in 12-hour format.

Why is MY layout preview not showing all my views?

If your layout preview ends up being too different from how the layout will actually look on devices, it can be very confusing. If, for example, you have a bunch of invisible views of which only one at a time can be visible, showing them all in the preview might be chaotic.


Video Answer


3 Answers

Comment your TextClock and refresh your preview, and then uncomment your TextClock. This will fix your issue.

I encountered this problem when I upgraded to AS 3.6.1

like image 163
Jaswant Singh Avatar answered Oct 16 '22 08:10

Jaswant Singh


A fix/workraound is to create a subclass of TextClock and add this into the created subclass:

@Override
protected void onAttachedToWindow() {
    try {
        super.onAttachedToWindow();
    } catch(Exception e)  {
    }
}

Source

like image 31
tgi_rxlph Avatar answered Oct 16 '22 08:10

tgi_rxlph


I had the same problem; my workaround was to convert the TextClock to a TextView via rightclick -> convert view as TextClock extends TextView, then the Layout renders, and after the Layout rendered I converted it back to a TextClock.

It's an ugly workaround - not a fix (you have to repeat it after closing Android Studio) - but at least I can see the layout now.

I will report it to Android Studio as a bug and hopefully they fix it

like image 3
JuKe Avatar answered Oct 16 '22 09:10

JuKe