简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM