android - 我放了一些广告和 AdMob 代码,但无法观看
问题描述
我正在创建一个应用程序并想在主页上添加一些 Google Ads。我按照谷歌所说的所有步骤操作,没有任何警告或错误,但是当我运行应用程序时,没有任何广告。
这是 MainActivity 代码:
public class MainActivity extends AppCompatActivity {
private FragmentTabHost tabHost;
private AdView mAdView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//adds
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
这是mainActivity的xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="35dp"
android:orientation="vertical"
android:gravity="center"
android:background="@drawable/mix">
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
<Button
android:id="@+id/natacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_bg_rounded_corners"
android:onClick="lanzarNatacion"
android:layout_marginTop="80dp"
android:textSize="20dp"
android:text="Natacion" />
<Button
android:id="@+id/carrera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="lanzarCarrera"
android:background="@drawable/button_bg_rounded_corners"
android:layout_marginTop="80dp"
android:textSize="20dp"
android:text="Carrera" />
</LinearLayout>
这是主要的代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="soloviev.perez.vicente.ritmos">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_mi_icono_"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:roundIcon="@mipmap/ic_mi_icono_"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".carrera" android:label="Calculadora ritmo de carrera" />
<activity android:name=".natacion" android:label="Calculadora ritmo de natacion" />
<activity android:name=".error" android:label="Error" android:theme="@android:style/Theme.Dialog" />
<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-4666536989991556~5464124026"/>
</application>
</manifest>
我添加了我必须添加的所有依赖项和存储库。
当我运行时,只会出现一个标签,上面写着“测试广告”,但应该会出现更多内容。
我使用的代码是 Google 提供的示例代码。
谢谢
解决方案
请在清单中添加网络权限
<uses-permission android:name="android.permission.INTERNET"/>
更改您的代码
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
和
mAdView = findViewById(R.id.adView)
AdRequest adRequest = AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build()
mAdView .loadAd(adRequest )
更新
记录错误代码
mAdView.setAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(int errorCode) {
Log.d("Error code",errorCode.toString());
}
}
并在此处检查错误代码
更新
在你的毕业典礼上
implementation 'com.google.android.gms:play-services-ads:15.0.1'
添加你的 build.gradle(Module:APP) 的结尾
repositories {
maven { url "https://jitpack.io" }
google()
jcenter()
}
推荐阅读
- jmeter - 无法在chrome中打开网址
- xml - XSLT 1.0 按子项的值对 xml 节点进行分组
- tensorflow - 尝试运行 keras Tuner 时,Google Colab 不断崩溃
- function - 如何指示函数变量的返回类型
- flutter - Flutter:避免 FlexibleSpaceBar 的不透明效果
- python - 如何使用 difflib 通过搜索列表中的元素来返回列表?
- javascript - JavaScript:修改 DOM 并用 JSON 数据填充它
- angular - angular-datetimepicker 没有反映正确的日期
- xamarin - 绑定 .Bind(TapGestureRecognizer.CommandProperty, nameof(BackArrowTapped)); 到网格不会调用我的 BackArrowTapped 命令
- python - 如何使用 python 请求库从页面中获取特定的标签元素