为什么AdMob没有在我的Android应用中显示任何广告(代码中没有错误)?

时间:2016-11-15 05:27:40

标签: android admob integration

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

我在Windows 7上使用Android Studio 1.2.2

Android SDK工具版本24.0.2

Android支持资源库第16版

Android支持库Rev. 22.2.1

Google Play服务第32版 Google Repository Rev. 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(模块:应用)

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`

请各位朋友,帮帮我吧!我非常渴望解决这个问题。

1 个答案:

答案 0 :(得分:0)

我认为你没有在admob上注册app。 按照这个步骤。

  1. 登录您的AdMob帐户。

  2. 转到“获利”标签。

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

    <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/