【问题标题】:Why AdMob is not showing any ads in my Android app(there are no errors in the code)?为什么 AdMob 没有在我的 Android 应用中展示任何广告(代码中没有错误)?
【发布时间】:2017-03-28 21:50:31
【问题描述】:

我非常努力地在 Android 应用中展示广告。我在网上搜索了很多 AdMob 集成教程,但没有一个对我有用。以下是完整的详细信息 -

我在 Windows 7 上使用 Android Studio 1.2.2

Android SDK 工具版本 24.0.2

Android 支持存储库修订版 16

Android 支持库修订版 22.2.1

Google Play 服务修订版 32 Google 存储库第 32 版

我尝试了很多方法,但没有一个对我有用。这是我的代码

MainActivity

package com.example.virender.myapplication;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
private static final String TOAST_TEXT = "Test ads are being shown. ";


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

AdView adView = (AdView) this.findViewById(R.id.adView);

// Request for Ads
AdRequest adRequest = new AdRequest.Builder()

// Add a test device to show Test Ads
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("B3EEABB8EE11C2BE770B684D95219ECB")
.build();

// Load ads into Banner Ads
adView.loadAd(adRequest);

// Toasts the test ad message on the screen. 
Toast.makeText(this, TOAST_TEXT, Toast.LENGTH_LONG).show();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.virender.adraj" >

<!-- Include required permissions for Google Mobile Ads to run. -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

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

<!-- This meta-data tag is required to use Google Play Services. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

<activity
android:name=".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> <!-- Include the AdActivity configChanges and theme. -->
<activity
android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
</application>

</manifest>

build.gradle(Module:app)

apply plugin: 'com.android.application'
android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

defaultConfig {
    applicationId "com.example.virender.adraj"
    minSdkVersion 9
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.google.android.gms:play-services:6.5.87'
}

Activity_main.xml

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<!-- view for AdMob Banner Ad -->
<com.google.android.gms.ads.AdView android:id="@+id/adView" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true" ads:adSize="SMART_BANNER"
    ads:adUnitId="@string/banner_ad_unit_id"/>

** 这是我的 logcat 生成的内容 **

11-15 10:13:29.116    6558-6558/? D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
11-15 10:13:30.585    6558-6558/com.example.virender.adraj I/dalvikvm﹕ Could not find method android.view.ViewGroup.onWindowSystemUiVisibilityChanged, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onWindowSystemUiVisibilityChanged
11-15 10:13:30.585    6558-6558/com.example.virender.adraj W/dalvikvm﹕ VFY: unable to resolve virtual method 13714: Landroid/view/ViewGroup;.onWindowSystemUiVisibilityChanged (I)V
11-15 10:13:30.585    6558-6558/com.example.virender.adraj D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0008
11-15 10:13:31.075    6558-6558/com.example.virender.adraj I/dalvikvm﹕ Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
11-15 10:13:31.075    6558-6558/com.example.virender.adraj W/dalvikvm﹕ VFY: unable to resolve virtual method 13710: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
11-15 10:13:31.075    6558-6558/com.example.virender.adraj D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0007
11-15 10:13:31.105    6558-6558/com.example.virender.adraj I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
11-15 10:13:31.105    6558-6558/com.example.virender.adraj W/dalvikvm﹕ VFY: unable to resolve virtual method 573: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
11-15 10:13:31.115    6558-6558/com.example.virender.adraj D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
11-15 10:13:31.165    6558-6558/com.example.virender.adraj I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
11-15 10:13:31.165    6558-6558/com.example.virender.adraj W/dalvikvm﹕ VFY: unable to resolve virtual method 595: Landroid/content/res/TypedArray;.getType (I)I
11-15 10:13:31.165    6558-6558/com.example.virender.adraj D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
11-15 10:13:31.475    6558-6558/com.example.virender.adraj I/dalvikvm﹕ Could not find method android.app.Notification$Builder.setPriority, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.a
11-15 10:13:31.475    6558-6558/com.example.virender.adraj W/dalvikvm﹕ VFY: unable to resolve virtual method 237: Landroid/app/Notification$Builder;.setPriority (I)Landroid/app/Notification$Builder;
11-15 10:13:31.485    6558-6558/com.example.virender.adraj D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0035
11-15 10:13:31.545    6558-6563/com.example.virender.adraj D/dalvikvm﹕ GC_CONCURRENT freed 214K, 5% free 7081K/7431K, paused 5ms+8ms
11-15 10:13:31.555    6558-6558/com.example.virender.adraj D/dalvikvm﹕ DexOpt: unable to opt direct call 0x00ce at 0x3f in Lcom/google/android/gms/common/GooglePlayServicesUtil;.a
11-15 10:13:31.585    6558-6558/com.example.virender.adraj W/GooglePlayServicesUtil﹕ Google Play services is missing.
11-15 10:13:31.655    6558-6558/com.example.virender.adraj W/GooglePlayServicesUtil﹕ Google Play services is missing.
11-15 10:13:31.855    6558-6558/com.example.virender.adraj I/Ads﹕ Starting ad request.
11-15 10:13:32.675    6558-6558/com.example.virender.adraj D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
11-15 10:13:33.655    6558-6567/com.example.virender.adraj I/dalvikvm﹕ Total arena pages for JIT: 11
11-15 10:13:33.655    6558-6567/com.example.virender.adraj I/dalvikvm﹕ Total arena pages for JIT: 12
11-15 10:13:33.665    6558-6567/com.example.virender.adraj I/dalvikvm﹕ Total arena pages for JIT: 13
11-15 10:13:33.665    6558-6567/com.example.virender.adraj I/dalvikvm﹕ Total arena pages for JIT: 14
11-15 10:13:33.675    6558-6567/com.example.virender.adraj I/dalvikvm﹕ Total arena pages for JIT: 15
11-15 10:13:33.675    6558-6567/com.example.virender.adraj I/dalvikvm﹕ Total arena pages for JIT: 16
11-15 10:13:33.675    6558-6567/com.example.virender.adraj I/dalvikvm﹕ Total arena pages for JIT: 17
11-15 10:13:33.795    6558-6567/com.example.virender.adraj I/dalvikvm﹕ Total arena pages for JIT: 18
11-15 10:13:34.108    6558-6594/com.example.virender.adraj D/dalvikvm﹕ DexOpt: --- BEGIN 'ads-1257843649.jar' (bootstrap=0) ---
11-15 10:13:34.305    6558-6594/com.example.virender.adraj D/dalvikvm﹕ DexOpt: --- END 'ads-1257843649.jar' (success) ---
11-15 10:13:34.305    6558-6594/com.example.virender.adraj D/dalvikvm﹕ DEX prep '/data/data/com.example.virender.adraj/cache/ads-1257843649.jar': unzip in 24ms, rewrite 199ms
11-15 10:13:35.646    6558-6563/com.example.virender.adraj D/dalvikvm﹕ GC_CONCURRENT freed 285K, 6% free 7246K/7687K, paused 6ms+5ms
11-15 10:13:35.675    6558-6558/com.example.virender.adraj D/webviewglue﹕ nativeDestroy view: 0x2febc8
11-15 10:13:36.165    6558-6642/com.example.virender.adraj I/dalvikvm﹕ Could not find method android.net.ConnectivityManager.isActiveNetworkMetered, referenced from method com.google.android.gms.internal.fv.<init>
11-15 10:13:36.165    6558-6642/com.example.virender.adraj W/dalvikvm﹕ VFY: unable to resolve virtual method 1110: Landroid/net/ConnectivityManager;.isActiveNetworkMetered ()Z
11-15 10:13:36.165    6558-6642/com.example.virender.adraj D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x00d9
11-15 10:13:36.355    6558-6558/com.example.virender.adraj I/dalvikvm﹕ Could not find method android.webkit.WebView.evaluateJavascript, referenced from method com.google.android.gms.internal.gu.evaluateJavascript
11-15 10:13:36.355    6558-6558/com.example.virender.adraj W/dalvikvm﹕ VFY: unable to resolve virtual method 14098: Landroid/webkit/WebView;.evaluateJavascript (Ljava/lang/String;Landroid/webkit/ValueCallback;)V
11-15 10:13:36.355    6558-6558/com.example.virender.adraj D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0016
11-15 10:13:46.366    6558-6605/com.example.virender.adraj W/Ads﹕ There was a problem getting an ad response. ErrorCode: 0
11-15 10:13:46.366    6558-6558/com.example.virender.adraj D/webviewglue﹕ nativeDestroy view: 0x31a598
11-15 10:13:46.536    6558-6558/com.example.virender.adraj W/Ads﹕ Failed to load ad: 0
11-15 10:19:31.346    6558-6563/com.example.virender.adraj D/dalvikvm﹕ `GC_CONCURRENT freed 347K, 7% free 7306K/7815K, paused 40ms+11ms`

请朋友们帮帮我!我真的很想解决这个问题。

【问题讨论】:

  • 有时加载需要时间
  • logcat显示“加载广告失败”
  • 你添加了MobileAds.initialize();方法?
  • 试试这个ads:adSize="BANNER"而不是这个ads:adSize="SMART_BANNER"
  • @BurhanuddinRashid 我已经尝试过 ads:adSize="SMART_BANNER" ,但它对我不起作用

标签: android admob integration


【解决方案1】:

我认为您没有在 admob 上注册应用。 请按照以下步骤操作。

  1. 登录您的AdMob 帐户。

  2. 转到“获利”标签。

  3. 创建应用并选择合适的平台。
  4. 选择广告格式并为广告单元命名。
  5. 创建单元名称后,它将生成广告单元 ID,例如广告 ID 将如下所示:ca-app-pub-1234567890000000/9874563210。
  6. 现在复制您的 id 并将其粘贴到 strings.xml 文件中

喜欢这个

<string name="banner_ad_unit_id">ca-app-pub-1234567890000000/9874563210</string>

在您的 onCreate() 活动方法中粘贴此代码。 确保您避免点击广告。这不会生成测试广告这是真实的广告。不要每次都点击广告。这将导致您的 admob 帐户被禁止。

AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);

你也可以参考这个链接

http://www.androidhive.info/2016/02/android-how-to-integrate-google-admob-in-your-app/

【讨论】:

  • 我已经在 AdMob 上创建了一个帐户,我也尝试过发布实时广告,但也没有成功。
  • 能否把MainActivity的完整代码贴出来,这里的代码就是贴出来的
  • 只需从您的代码中删除这两行。 .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .addTestDevice("B3EEABB8EE11C2BE770B684D95219ECB") 并尝试。我认为这可能有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-24
  • 1970-01-01
  • 2020-10-10
  • 1970-01-01
  • 2011-08-23
相关资源
最近更新 更多