
[英]What is the reason to show 26 no line error? I am using kotlin 1.4.21 and android studio 4.1.1 .Please give me a solution
[英]I want to show Native_Ads_view In XML file in Android Studio but it give me error
基本上我的问题是,当我在 XML 文件中添加 native_ads_view 时,它给了我错误(“Required XML_attribute “adSize” was missing”)。
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
xmlns:ads="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#72B2D1"
tools:context=".SplashScreen">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
app:adSize="BANNER"
app:adUnitId="ca-app-pub-3940256099942544/6300978111"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</com.google.android.gms.ads.AdView>
<com.google.android.gms.ads.NativeExpressAdView
android:id="@+id/sndNativeAds"
ads:adUnitId="ca-app-pub-3940256099942544/2247696110"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="300x200"
app:layout_constraintBottom_toTopOf="@+id/btn_move"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/adView">
</com.google.android.gms.ads.NativeExpressAdView>
<Button
android:id="@+id/btn_move"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="Move To Next"
android:textSize="20sp"
android:background="#000000"
android:textColor="#fff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
错误
Required XML_attribute "adSize" was missing
我找到了一个解决方案,如果您像这样使用它,它将起作用:
<com.google.android.gms.ads.NativeExpressAdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="FULL_WIDTHx80"
ads:adUnitId="@string/native_ad_unit"/>
或者当我以编程方式添加NativeExpressAdView
并将其从XML
删除时它已经解决了,如下所示。
nativeAd = new NativeExpressAdView(this);
int widthInDP = context.getResources().getConfiguration().screenWidthDp;
widthInDP -= context.getResources().getDimension(R.dimen.activity_horizontal_margin);
nativeAd.setAdSize(new AdSize(widthInDP, 250));
nativeAd.setAdUnitId("myAdUnitId");
// Create an ad request.
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
// Optionally populate the ad request builder.
adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
// Add the NativeExpressAdView to the view hierarchy.
layout.addView(nativeAd);
// Start loading the ad.
nativeAd.loadAd(adRequestBuilder.build());
或者它可以通过使缓存无效并重新启动您的 android studio 来解决,或者尝试将大小更改为ads:adSize="300x250"
肯定会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.