添加AdMob后,Android应用程序启动失败

3
我正在尝试在我的应用程序中实现admob,但在添加后,应用程序无法启动。
以下是活动代码: http://code.google.com/p/zhuang-dict/source/browse/trunk/ZhuangDict/src/cn/wangdazhuang/zdict/ZhuangDictActivity.java 主要的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads= "http://schemas.android.com/apk/lib/com.google.ads"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    >
        <ImageButton
            android:id="@+id/ts"
            android:contentDescription="@string/tts_contentDescription"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" 
            android:src="@android:drawable/ic_btn_speak_now"
        />
        <ImageButton
            android:id="@+id/search"
            android:contentDescription="@string/search_contentDescription"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/ts"
            android:src="@android:drawable/ic_search_category_default"
        />
        <AutoCompleteTextView
            android:id="@+id/edit"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:focusable="true"
            android:singleLine="true"
            android:layout_toLeftOf="@id/search"
            android:hint="@string/edit_hint"
        />
    </RelativeLayout>
    <WebView
        android:id="@+id/result"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />
    <com.google.ads.AdView android:id="@+id/adView"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     ads:adUnitId="myId"
                     ads:adSize="BANNER"
                     ads:loadAdOnCreate="true"
     />
</LinearLayout>

2
发布logcat。这样可以帮助确定原因。 - undefined
二进制XML文件第43行:解析com.google.ads.AdView类时出错。去看看第43行,可能是一些格式错误的XML。 - undefined
第43行是上面的com.google.ads.adView代码。 - undefined
4个回答

4
你是否已将此添加到你的清单文件中?
    <activity android:name="com.google.ads.AdActivity"
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

如Google教程所示:

    <com.google.ads.AdView android:id="@+id/adView"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     ads:adUnitId="MY_AD_UNIT_ID"
                     ads:adSize="BANNER"
                     ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
                     ads:loadAdOnCreate="true"/>

adUnitId 必须是您应用的 Id,您设置了此 ID 或者像上面代码中所示真的设置为 "myid"?

编辑

这里有一些额外信息,似乎 AdMob 库未捆绑到您的项目中。这可能由许多不同的原因引起。阅读以下步骤并确保您已经做到了,如果没有,请执行以下操作:

更新 Eclipse 和 Android SDK:

  1. 以管理员身份打开 Eclipse
  2. 转到 帮助-->检查更新 并运行更新
  3. 勾选下一个窗口中显示的所有内容。虽然不是必需的,但升级所有内容不会有什么坏处
  4. 接受条款和许可证,然后单击完成...更新将开始,需要一些时间
  5. 如果更新完成,请以管理员身份重新启动 Eclipse。可能会收到无法完成所有更新的错误消息...继续进行

更新 Android SDK:

  1. 在 Eclipse 中转到 窗口-->Android SDK 管理器
  2. 在“状态”列中,您可以看到是否有可用的更新。选择所有更新...
  3. 完成后,再次重新启动 Eclipse

使用 Admob - 放置 jar 文件并设置路径:

  1. 从 Google 网站下载 AdMob SDK(可能因国家而异) https://developers.google.com/mobile-ads-sdk/download?hl=de
  2. 将文件解压缩到任意目录中
  3. 在 Eclipse 中的项目中创建一个名为 "libs" 的文件夹。 确保文件夹命名为 "libs" 而不是 "lib"。这种错误经常会发生


  4. 将 admob 文件夹中的 jar 文件复制到您的项目中,放在 libs 文件夹中

  5. 右键单击位于 lib 文件夹中的 jar 文件,选择“构建路径”-->添加到构建路径
  6. 为确保一切都已完成,请右键单击您的项目并选择属性
  7. 选择 Java Build Path,并转到库选项卡
  8. 选择添加外部 jar 并从您的 admob 文件夹中选择此 jar
  9. 选择确定

开始

  1. put this into your Manifest.xml

    <activity android:name="com.google.ads.AdActivity"
       android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
     </application>
    
您的Manifest.xml应该类似于以下内容:
       <?xml version="1.0" encoding="utf-8"?>
         <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.company"
          android:versionCode="1" android:versionName="1.0">
       <application android:icon="@drawable/icon" android:label="@string/app_name"
           android:debuggable="true">
       <activity android:label="@string/app_name" android:name="BannerExample">
           <intent-filter>
           <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
             </intent-filter>
       </activity>
      <activity android:name="com.google.ads.AdActivity"
             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
        </application>
        </manifest>

注意:将这行放在您的活动下方,而不是内部。
  1. set permissions inside the manifest.xml

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    
  2. create the xml layout for example like shown in the tutorial

    <?xml version="1.0" encoding="utf-8"?>
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
        <com.google.ads.AdView android:id="@+id/adView"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     ads:adUnitId="MY_AD_UNIT_ID"
                     ads:adSize="BANNER"
                     ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
                     ads:loadAdOnCreate="true"/>
       </LinearLayout>
    
  3. Additionally, if the steps above did not work, You could do this inside Your Activity where You want to show the admob. Refer to Your admob xml id in onCreate() of Your Activity:

        public class AdMobActivity extends Activity {
    
       private AdView adView;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
    
            // Create the adView
       adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);
    
           // Lookup your LinearLayout assuming it’s been given
          // the attribute android:id="@+id/mainLayout"
        LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
    
           // Add the adView to it
         layout.addView(adView);
    
           // Initiate a generic request to load it with an ad
        adView.loadAd(new AdRequest());
    
         }
    

在我的应用程序中,我已经同时使用了XML和代码来完成。通常情况下,只需要在XML或代码中完成其中一种即可,但同时使用两种方法也可以正常工作。


是的,我可以将它添加到我的自己的ID中。实际上,我是按照http://www.wikihow.com/Add-Advertisements-to-Your-Android-Applications-Using-Admob上的指南进行操作的。 - undefined
好的,我已经编辑了你的ID,最好没人看到这个...但是现在,让我感到烦恼的是webView。你将WebView的高度设置为fill_parent,这超出了相对布局并填满了整个空间。你可以尝试移除这个webView然后再次运行你的活动。如果你在代码中使用了这个webView,请不要忘记将其注释掉以避免错误。 - undefined
我的意思是你的Java代码,public class AdMobActivity。我不确定该怎么做。应该创建一个新的Java文件还是将其粘贴到ZhuangDictActivity.java中。我都试过了,但是出现了错误。我选择了"Android",但意外的是,没有添加任何库 http://i6.photobucket.com/albums/y248/zfc/Screenshot4_zps81efb68e.png 我到底做错了什么? - undefined
不,我表达错了,你发给我的图片是可以的。里面不能有库,只有在你想从现有项目中导入时才需要。所以没问题。你必须引用活动中的adview来显示admob广告。如果你已经在main.xml中指定了adview,请在依赖main.xml的活动中声明adview。请在onCreate()方法内进行操作。 - undefined
是的,我会尝试帮助您,但不确定能否解决问题。也许需要一点时间……没有金钱的交易,我们在这里互相帮助 :) opiatefuchs@googlemail.com - undefined
显示剩余11条评论

1
请确保您正在初始化AdMob,否则您将会遇到以下错误:
无法获取isAdIdFakeForDebugLogging
MobileAds.initialize(this, AD_MOB_ID);

0
尝试以下步骤:
1. 在项目中创建一个名为“libs”的文件夹。 2. 将Google AdMobs的“jar”文件添加到“libs”文件夹中。 3. 从“libs”文件夹导入AdMob库。 4. 确认通过以下方式验证新方法是否有效:右键单击项目 -> 构建路径 -> 配置构建路径 -> 切换到“库”选项卡,并确认Google AdMobs库在列表中。
我在升级到ADT的第15或16个版本后遇到了这个问题,我记不清是哪个版本了。
在执行上述操作之前,别忘了删除对该库的早期引用。

@user2190227:抱歉回复晚了。你能发一下你的Activity代码吗?我不确定这个错误是否与AdMob有关。 - undefined
我的活动代码基本上是基于这个源码http://code.google.com/p/zhuang-dict/source/browse/trunk/ZhuangDict/src/cn/wangdazhuang/zdict/ZhuangDictActivity.java。 - undefined

0

如果您在调色板中添加了Google AdView并且应用程序停止工作。

在Android Studio中转到“Gradle Scripts”文件夹

找到build.gradle(Module:app)

从文件末尾删除此行

implementation 'com.google.android.gms:play-services-ads:18.1.1'

保存文件并重新构建项目。一切将再次正常工作

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.android.gms:play-services-ads:18.1.1'
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接