Don't Create Threads in the preview window - Huawei Ads Implementation

181 Views Asked by At

I'm implementing the huawei ads in the android Application. I have go through all the https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/publisher-service-banner-0000001050066915 and successfully implemented the ads but android studio preview is giving me error and is not displaying the preview when adding the huawei ads in the xml. After Removing the Huawei Ads preview is showing.

Following is the my layout:

 <?xml version="1.0" encoding="utf-8"?>
<com.huawei.hms.ads.nativead.NativeView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/native_video_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:background="#FFFFFF"
    android:orientation="vertical">
    <com.huawei.hms.ads.nativead.MediaView
        android:id="@+id/ad_media"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_200sdp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/_50sdp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:background="@color/white">
        <TextView
            android:id="@+id/ad_title"
            android:layout_width="180dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="16dp"
            android:alpha="1"
            android:textColor="#000000"
            android:textSize="@dimen/hiad_text_13_sp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>
        <TextView
            android:id="@+id/ad_source"
            android:layout_width="wrap_content"
            android:layout_height="19dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="2dp"
            android:layout_marginBottom="16dp"
            android:alpha="0.6"
            android:text="asdfasj"
            android:maxWidth="158dp"
            android:textColor="#666666"
            android:textSize="@dimen/hiad_text_12_sp"
            app:layout_constraintStart_toStartOf="@id/ad_title"
            app:layout_constraintEnd_toStartOf="@id/ad_flag"
            app:layout_constraintTop_toBottomOf="@id/ad_title"
        />

        <TextView
            android:id="@+id/ad_flag"
            android:layout_width="20dp"
            android:layout_height="14dp"
            android:background="@drawable/native_flag_rounded_corners_shape"
            android:text="@string/ad_flag"
            android:textColor="#FFFFFF"
            android:textSize="8sp"
            android:layout_marginStart="@dimen/_10sdp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@id/ad_source"
            app:layout_constraintStart_toEndOf="@id/ad_source"
            app:layout_constraintTop_toTopOf="@id/ad_source" />

        <Button
            android:id="@+id/ad_call_to_action"
            android:layout_width="72dp"
            android:layout_height="26dp"
            android:layout_alignParentEnd="true"
            android:background="@drawable/native_button_rounded_corners_shape"
            android:textColor="#FFFFFF"
            android:textSize="10sp"
            android:layout_marginEnd="@dimen/_20sdp"
            app:layout_constraintBottom_toBottomOf="@+id/ad_source"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</com.huawei.hms.ads.nativead.NativeView>

Following is the error I m encountering with enter image description here When I remove the root tag to Constraint Layout. Preview is visible on Screen and all errors are gone. Inside those two tags I m facing the following Exception:

FirstException: Don't Create Threads in Preview

java.lang.IllegalStateException: It is not allowed to create new threads in the preview   at com.huawei.hms.ads.kn.newThread  at java.util.concurrent.ThreadPoolExecutor$Worker.(ThreadPoolExecutor.java:623)   at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:912)   at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1343)   at com.huawei.hms.ads.km.Code  at com.huawei.hms.ads.km.I  at com.huawei.hms.ads.lj.Code  at com.huawei.hms.ads.lj.V  at com.huawei.hms.ads.dl.Z  at com.huawei.hms.ads.dl.V  at com.huawei.hms.ads.dl.Code  at com.huawei.openalliance.ad.views.PPSNativeView.Code  at com.huawei.openalliance.ad.views.PPSNativeView.  at com.huawei.hms.ads.nativead.NativeView.  at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(NativeConstructorAccessorImpl.java:-2)   at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)   at jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)   at java.lang.reflect.Constructor.newInstance(Constructor.java:490)   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)   at android.view.LayoutInflater.inflate(LayoutInflater.java:657)   at android.view.LayoutInflater.inflate(LayoutInflater.java:499)

Second Exception: Failed to instantiate One Or more Class The following classes could not be instantiated: - com.huawei.hms.ads.nativead.MediaView (Open Class, Show Exception, Clear Cache) - com.huawei.openalliance.ad.views.VideoView (Open Class, Show Exception, Clear Cache) Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE. If this is an unexpected error you can also try to build the project, then manually refresh the layout. Exception Details java.lang.NullPointerException   at com.huawei.openalliance.ad.views.NativeVideoView.Code  at com.huawei.openalliance.ad.views.NativeVideoView.  at com.huawei.hms.ads.nativead.MediaView.Code  at com.huawei.hms.ads.nativead.MediaView.  at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(NativeConstructorAccessorImpl.java:-2)   at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)   at jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)   at java.lang.reflect.Constructor.newInstance(Constructor.java:490)   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)   at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1121)   at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)   at android.view.LayoutInflater.rInflate(LayoutInflater.java:1095)   at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)   at android.view.LayoutInflater.inflate(LayoutInflater.java:680)   at android.view.LayoutInflater.inflate(LayoutInflater.java:499)

I have invalidate the Cache and restart the Android Studio but nothing has worked yet. Can any one help me to render the preview. Any help would be appreciated. Thanks

1

There are 1 best solutions below

6
zhangxaochen On

Currently, the Ads kit SDK references the HMS base SDK code, but seems you have not integrate the HMS base SDK.

As a result, this following error occurs during preview.

enter image description here

If you choose not to integrate the HMS base SDK, the Ads SDK function is not affected but will be affected during preview.

So there are two options for your reference : One way is you can try to integrate the hms base sdk, or another way is to ignore this preview error, because this will not affect the actual operation.