You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

集成Admob时触发RuntimeException,请求技术排查协助

解决AdMob横幅广告加载时的IllegalStateException问题

嘿,作为Android新手遇到AdMob集成问题太正常了,咱们一步步拆解你碰到的错误:java.lang.IllegalStateException: The adSize and adUnitId must be set before loadAd is called. 这个错误核心是AdView没正确读取到广告尺寸和单元ID,咱们从代码里找问题:

1. 先修XML布局里的关键错误

你的AdView布局有两处致命问题:

  • 命名空间写错:RelativeLayout里的xmlns:ads被误设成了http://schemas.android.com/tools,正确的应该是http://schemas.android.com/apk/res-auto,而且AdView里不需要重复声明这个命名空间。
  • adUnitId拼写错误:你写的是ads:adUnitid(小写i),官方属性名是ads:adUnitId(大写I),这个拼写失误直接导致AdView识别不了你的广告单元ID。

修正后的完整XML代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content">

    <com.google.android.gms.ads.AdView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerHorizontal="true" 
        android:layout_marginTop="500dp" 
        ads:adSize="BANNER" 
        android:id="@+id/adView" 
        ads:adUnitId="ca-app-pub-8871036677328731/4777520829">
    </com.google.android.gms.ads.AdView>
</RelativeLayout>

2. 修复AndroidManifest.xml的应用ID配置

你Manifest里的第一个meta-data完全写错了!正确的AdMob应用ID配置应该是name="com.google.android.gms.ads.APPLICATION_ID",而不是把ID字符串直接塞到name字段里。修正后的Manifest相关代码:

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme">

    <!-- 正确的AdMob应用ID配置 -->
    <meta-data 
        android:name="com.google.android.gms.ads.APPLICATION_ID" 
        android:value="ca-app-pub-8871036677328731~4808121041" />
    
    <meta-data 
        android:name="com.google.android.gms.ads.AD_MANAGER_APP" 
        android:value="true"/>

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

另外提一句:你的MainActivity的intent-filter里多了个android.intent.action.VIEW,如果不是业务需要,建议删掉,只保留MAIN和LAUNCHER就行。

3. 优化MainActivity的初始化代码

你初始化MobileAds的时候多套了一层引号,直接去掉就行,另外现在AndroidX支持泛型findViewById,不用强制转换了:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // 去掉多余的引号
    MobileAds.initialize(this, "ca-app-pub-8871036677328731~4808121041");
    mAdView = findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
}

最后验证步骤

  1. 先清理项目缓存(Build -> Clean Project),再重新构建(Build -> Rebuild Project)
  2. 测试阶段建议用AdMob官方测试广告单元ID(比如ca-app-pub-3940256099942544/6300978111),避免触发真实广告的限制
  3. 确认你在AdMob后台已经正确创建了对应adUnitId的横幅广告单元

这样改完之后,那个烦人的IllegalStateException应该就消失了~

内容的提问来源于stack exchange,提问作者Jamal H.A

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:44:00