[英]Android fragment layout not loading when showing an Admob interstitial ad in OnCreateView
[英]Android - trouble loading ad using admob with adRquest
我在将广告整合到我的应用程序时遇到了一些麻烦。 我收到以下错误:构造函数AdRequest()。 在线:adView.loadAd(new AdRequest());
这是包含广告视图的xml文件:
<?xml version="1.0"?>
<RelativeLayout tools:ignore="MergeRootFrame"
tools:context="com.example.blueshift.MainActivity"
android:layout_height="match_parent"
android:layout_width="match_parent" android:id="@+id/container"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads">
<com.example.blueshift.CustomView android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/custom_view"/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/textView1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="djknfjndj" android:layout_marginBottom="128dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/textView2"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="djknfjndj" android:layout_marginBottom="160dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/button1"
android:text="Play Again"
android:layout_marginBottom="114dp"
android:layout_centerHorizontal="true"
android:visibility="gone"
android:layout_above="@+id/textView1"/>
<com.google.android.gms.ads.AdView android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="id"
ads:loadAdsOnCreate="true"/>
</RelativeLayout>
这是我的android清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.blueshift"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
<activity
android:name="com.example.blueshift.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我用来尝试加载包含错误的添加的Java代码:
AdView adView = (AdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
谢谢你的帮助。
更新:
遵循Kishan的建议并实施了他的更改,但是现在我得到了错误:adView.loadAd(adRequest)行中的AdView类型未定义loadAd(AdRequest)方法; 有什么建议吗?
您尚未添加Adrequest,因此无法使用此功能:
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
有关加载广告的更多详细信息,请使用此链接。
关于错误消息:
在adView.loadAd(adRequest)行上未为类型AdView定义方法loadAd(AdRequest);
问题似乎是您设置了错误的xml名称空间:
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
这是指旧版Admob,而您想使用新版admob,请改用:
xmlns:ads="http://schemas.android.com/apk/res-auto"
您还可以在“活动”代码中检查AdView的导入,它需要满足以下条件:
import com.google.android.gms.ads.AdView
还要从xml的AdView中删除此属性:
ads:loadAdOnCreate="true"
因为该功能仅适用于旧版Admob,并且您已经以编程方式加载了广告。
有关更多信息,请参阅此指南: https : //developers.google.com/mobile-ads-sdk/docs/admob/android/banner
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.