广告不会显示,android(AdMob)

时间:2014-08-31 16:31:45

标签: android admob

我一直试图让我的广告显示,但它不会显示....我只是继续得到以下错误,但我不确定他们的意思。我已正确导入google-play服务。

任何帮助都会很棒,谢谢!

更新

我已经解决了。

导致它停止的错误是'没有足够的空间',这是由于我使用线性布局。切换到相对布局后,它工作正常。

Main.java:

package com.example.f;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
//import com.google.android.gms.samples.ads.R;
//import com.google.android.gms.samples.ads.ToastAdListener;


public class MainActivity extends ActionBarActivity {

     private AdView adView;
     //This is the unit ID I get from admob account right?
     private static final String AD_UNIT_ID = "";

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

        // Create an ad.
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(AD_UNIT_ID);
        LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
        layout.addView(adView);

        AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
        .build();

    // Start loading the ad in the background.
    adView.loadAd(adRequest);  

    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onResume() {
      super.onResume();
      if (adView != null) {
        adView.resume();
      }
    }

    @Override
    public void onPause() {
      if (adView != null) {
        adView.pause();
      }
      super.onPause();
    }

    /** Called before the activity is destroyed. */
    @Override
    public void onDestroy() {
      // Destroy the AdView.
      if (adView != null) {
        adView.destroy();
      }
      super.onDestroy();
    }
}

main.xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
   android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.f.MainActivity" >

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

    <com.google.android.gms.ads.AdView android:id="@+id/adView"
                           android:layout_width="match_parent"
                           android:layout_height="wrap_content"
                           ads:adSize="BANNER"
                           ads:adUnitId=""

                           />

</LinearLayout>

logcat的:

08-31 17:32:36.309: W/ApplicationPackageManager(8370): getCSCPackageItemText()
08-31 17:32:36.429: D/dalvikvm(8370): DexOpt: --- BEGIN 'ads-1174894372.jar' (bootstrap=0) ---
08-31 17:32:36.439: I/Ads(8370): Starting ad request.
08-31 17:32:36.439: I/Ads(8370): Use AdRequest.Builder.addTestDevice("4F1AF66A4340D91DEDBA62394D3A289B") to get test ads on this device.
08-31 17:32:36.449: I/Ads(8370): Please set theme of AdActivity to @android:style/Theme.Translucent to enable transparent background interstitial ad.
08-31 17:32:36.459: V/WebViewChromium(8370): Binding Chromium to the background looper Looper (main, tid 1) {42d13f70}
08-31 17:32:36.459: I/chromium(8370): [INFO:library_loader_hooks.cc(112)] Chromium logging enabled: level = 0, default verbosity = 0
08-31 17:32:36.459: I/BrowserProcessMain(8370): Initializing chromium process, renderers=0
08-31 17:32:36.469: W/chromium(8370): [WARNING:proxy_service.cc(888)] PAC support disabled because there is no system implementation
08-31 17:32:36.469: W/ApplicationPackageManager(8370): getCSCPackageItemText()
08-31 17:32:36.479: I/Adreno-EGL(8370): <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build:  (CL3869936)
08-31 17:32:36.479: I/Adreno-EGL(8370): OpenGL ES Shader Compiler Version: 17.01.11.SPL
08-31 17:32:36.479: I/Adreno-EGL(8370): Build Date: 01/17/14 Fri
08-31 17:32:36.479: I/Adreno-EGL(8370): Local Branch: 
08-31 17:32:36.479: I/Adreno-EGL(8370): Remote Branch: 
08-31 17:32:36.479: I/Adreno-EGL(8370): Local Patches: 
08-31 17:32:36.479: I/Adreno-EGL(8370): Reconstruct Branch: 
08-31 17:32:36.549: D/dalvikvm(8370): DexOpt: --- END 'ads-1174894372.jar' (success) ---
08-31 17:32:36.549: D/dalvikvm(8370): DEX prep '/data/data/com.example.f/cache/ads-1174894372.jar': unzip in 0ms, rewrite 120ms
08-31 17:32:36.579: E/GooglePlayServicesUtil(8370): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
08-31 17:32:36.589: W/Ads(8370): Not enough space to show ad. Needs 320x50 dp, but only has 286x535 dp.
08-31 17:32:36.619: D/OpenGLRenderer(8370): Enabling debug mode 0
08-31 17:32:36.689: W/Ads(8370): Not enough space to show ad. Needs 320x50 dp, but only has 286x535 dp.
08-31 17:32:36.699: I/dalvikvm(8370): Total arena pages for JIT: 11
08-31 17:32:36.699: I/dalvikvm(8370): Total arena pages for JIT: 12
08-31 17:32:36.699: I/dalvikvm(8370): Total arena pages for JIT: 13
08-31 17:32:36.699: I/dalvikvm(8370): Total arena pages for JIT: 14
08-31 17:32:36.699: E/GooglePlayServicesUtil(8370): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
08-31 17:32:36.729: E/GooglePlayServicesUtil(8370): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
08-31 17:32:36.739: E/GooglePlayServicesUtil(8370): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
08-31 17:32:38.531: W/Ads(8370): Not enough space to show ad. Needs 320x50 dp, but only has 286x535 dp.
08-31 17:32:38.531: I/Ads(8370): Scheduling ad refresh 60000 milliseconds from now.
08-31 17:32:38.531: I/Ads(8370): Ad finished loading.
08-31 17:33:38.540: I/Ads(8370): Starting ad request.
08-31 17:33:38.540: I/Ads(8370): Use AdRequest.Builder.addTestDevice("4F1AF66A4340D91DEDBA62394D3A289B") to get test ads on this device.
08-31 17:33:38.550: I/Ads(8370): Please set theme of AdActivity to @android:style/Theme.Translucent to enable transparent background interstitial ad.
08-31 17:33:38.580: W/Ads(8370): Not enough space to show ad. Needs 320x50 dp, but only has 286x535 dp.
08-31 17:33:38.590: E/GooglePlayServicesUtil(8370): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
08-31 17:33:38.610: E/GooglePlayServicesUtil(8370): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
08-31 17:33:38.650: E/GooglePlayServicesUtil(8370): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
08-31 17:33:38.660: E/GooglePlayServicesUtil(8370): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
08-31 17:33:39.901: I/Ads(8370): Scheduling ad refresh 60000 milliseconds from now.
08-31 17:33:39.901: I/Ads(8370): Ad finished loading.
08-31 17:33:39.911: W/Ads(8370): Not enough space to show ad. Needs 320x50 dp, but only has 286x535 dp.
08-31 17:33:39.911: W/Ads(8370): Not enough space to show ad. Needs 320x50 dp, but only has 286x535 dp.
08-31 17:33:39.931: W/Ads(8370): Not enough space to show ad. Needs 320x50 dp, but only has 286x535 dp.
08-31 17:33:39.931: W/Ads(8370): Not enough space to show ad. Needs 320x50 dp, but only has 286x535 dp.
08-31 17:33:39.951: W/Ads(8370): Not enough space to show ad. Needs 320x50 dp, but only has 286x535 dp.
08-31 17:33:39.951: W/Ads(8370): Not enough space to show ad. Needs 320x50 dp, but only has 286x535 dp.
08-31 17:33:40.001: W/Ads(8370): Not enough space to show ad. Needs 320x50 dp, but only has 286x535 dp.
08-31 17:34:39.899: I/Ads(8370): Starting ad request.
08-31 17:34:39.899: I/Ads(8370): Use AdRequest.Builder.addTestDevice("4F1AF66A4340D91DEDBA62394D3A289B") to get test ads on this device.
08-31 17:34:39.909: I/Ads(8370): Please set theme of AdActivity to @android:style/Theme.Translucent to enable transparent background interstitial ad.
08-31 17:34:39.950: E/GooglePlayServicesUtil(8370): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
08-31 17:34:40.000: E/GooglePlayServicesUtil(8370): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
08-31 17:34:40.020: E/GooglePlayServicesUtil(8370): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
08-31 17:34:40.030: E/GooglePlayServicesUtil(8370): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
08-31 17:34:41.321: I/dalvikvm(8370): Total arena pages for JIT: 15
08-31 17:34:41.531: W/Ads(8370): Not enough space to show ad. Needs 320x50 dp, but only has 286x535 dp.
08-31 17:34:41.541: I/Ads(8370): Scheduling ad refresh 60000 milliseconds from now.
08-31 17:34:41.541: I/Ads(8370): Ad finished loading.
08-31 17:34:41.551: W/Ads(8370): Not enough space to show ad. Needs 320x50 dp, but only has 286x535 dp.
08-31 17:34:41.551: W/Ads(8370): Not enough space to show ad. Needs 320x50 dp, but only has 286x535 dp.
08-31 17:34:41.611: W/Ads(8370): Not enough space to show ad. Needs 320x50 dp, but only has 286x535 dp.

3 个答案:

答案 0 :(得分:0)

问题是Admob广告需要 320X50 dp 空间才能在您的应用中展示

如果您尝试更改这些值,则使用了relativelayout但

android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"

位于 dimens.xml ,将其更改为 50dp ,例如,您会看到广告不会显示。

这里的重点是你使用的是什么样的布局 重要的是确保空间 320X50 dp ,这可以在 dimens.xml

中找到

答案 1 :(得分:0)

AdSize.BANNER是320X50 dp。

对于较小的设备,而不是

adView.setAdSize(AdSize.BANNER);

您可以使用:

adView.setAdSize(new AdSize(286, 50));

答案 2 :(得分:0)

我有一个类似的问题,从这一行:

  

没有足够的空间来展示广告。需要320x50 dp,但只有286x535 dp。

上面的日志显示有足够的​​高度但宽度不够。因此,您必须确保下面列出的属性允许您的广告显示足够的宽度。因此,您可以将前两个设置为零,如下所示,并为最后一个做适当的事情。

prepareForSegue