Admob横幅广告未在应用中显示

时间:2014-06-20 17:38:11

标签: android android-fragments

我的应用中没有显示任何广告,请帮忙,下面是代码。我搜索了很多,但我能看到的只是我的代码中没有错误或可能是我不知道错误在哪里。

activity_main

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >

    <RelativeLayout xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <com.google.android.gms.ads.AdView
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="ca-app-pub-33xxxxxxxxxxxxxxxxxxxxx"/>
        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="@null"
            android:dividerHeight="0dp">
        </ListView>    
    </RelativeLayout>
</android.support.v4.view.ViewPager>

MainActivity

package com.nyt.ilm.ilmsarf;

import java.util.ArrayList;
import java.util.List;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.nyt.ilm.ilmsarf.P1.OnFragmentInteractionListener;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.ViewTreeObserver;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class MainActivity extends FragmentActivity implements
            OnFragmentInteractionListener {

    ViewPager viewpager;
    Context mContext = MainActivity.this;
    SharedPreferences appPreferences;
    boolean isAppInstalled = false;
    /* Your ad unit id. Replace with your actual ad unit id. */
    public boolean adViewHeightSet = false;

    @Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);

        appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        isAppInstalled = appPreferences.getBoolean("isAppInstalled", false);
        if (isAppInstalled == false) {
            // add shortcutIcon code here
        Intent shortcutIntent = new Intent(getApplicationContext(),
            MainActivity.class);

        shortcutIntent.setAction(Intent.ACTION_MAIN);
        // shortcutIntent is added with addIntent
        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
                addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "IlmSarf");
                addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                        Intent.ShortcutIconResource.fromContext(
                                getApplicationContext(), R.drawable.ic_launcher));

        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
                // finally broadcast the new Intent
                getApplicationContext().sendBroadcast(addIntent);
                // finally isAppInstalled should be true.
                SharedPreferences.Editor editor = appPreferences.edit();
                editor.putBoolean("isAppInstalled", true);
                editor.commit();
        }

        setContentView(R.layout.activity_main);
        viewpager = (ViewPager) findViewById(R.id.pager);
        FragmentPagerAdapter fm = new MyPageAdapter(getSupportFragmentManager());
        viewpager.setAdapter(fm);
        viewpager.setCurrentItem(fm.getCount() - 1);

        // Ad Banner

        AdView adView = (AdView) this.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);

        adView.getViewTreeObserver().addOnGlobalLayoutListener(
                    new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            if (adViewHeightSet) { return; }

            AdView adView = (AdView) findViewById(R.id.adView);
            ListView list = (ListView) findViewById(R.id.list);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
            params.setMargins(0, 0, 0, adView.getHeight() + 15);
            list.setLayoutParams(params);
            adViewHeightSet = true;
        }
        });

    }

    @Override
    public void onFragmentInteraction(Uri uri) {
        // TODO Auto-generated method stub

    }

}
class MyPageAdapter extends FragmentPagerAdapter {
    private List<Fragment> fragments;

    public MyPageAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
        this.fragments = new ArrayList<Fragment>();
        fragments.add(new P1());
        fragments.add(new P2());
        fragments.add(new P3());
        fragments.add(new P4());
        fragments.add(new P5());
        fragments.add(new P6());
        fragments.add(new P7());
        fragments.add(new P8());
        fragments.add(new P9());
        fragments.add(new P10());
        fragments.add(new P11());
        fragments.add(new P12());
        fragments.add(new P13());
        fragments.add(new P14());
        fragments.add(new P15());
        fragments.add(new P16());
        fragments.add(new P17());
        fragments.add(new P18());
        fragments.add(new P19());
        fragments.add(new P20());
        fragments.add(new P21());
        fragments.add(new P22());
        fragments.add(new P23());
        fragments.add(new P24());
        fragments.add(new P25());
        fragments.add(new P26());
        fragments.add(new P27());
        fragments.add(new P28());
        fragments.add(new P29());
        fragments.add(new P30());
        fragments.add(new P31());
        fragments.add(new P32());
        fragments.add(new P33());
        fragments.add(new P34());
        fragments.add(new P35());
        fragments.add(new P36());
        fragments.add(new P37());
        fragments.add(new P38());
        fragments.add(new P39());
        fragments.add(new P40());
        fragments.add(new P41());
        fragments.add(new P42());
        fragments.add(new P43());
        fragments.add(new P44());
        fragments.add(new P45());
        fragments.add(new P46());
        fragments.add(new P47());
        fragments.add(new P48());
        fragments.add(new P49());
        fragments.add(new P50());
        fragments.add(new P51());
        fragments.add(new P52());
        fragments.add(new P53());
        fragments.add(new P54());
        fragments.add(new P55());
        fragments.add(new P56());
        fragments.add(new P57());

    }

    @Override
    public Fragment getItem(int position) {
        // TODO Auto-generated method stub
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return fragments.size();
    }

}

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.nyt.ilm.ilmsarf"
    android:versionCode="3"
    android:versionName="1.3" xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
    <uses-permission android:name='com.android.launcher.permission.UNINSTALL_SHORTCUT'/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>
        <activity android:label="@string/app_name" android:name="adview" />
        <activity
            android:name="com.nyt.ilm.ilmsarf.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>
        <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
    </application>
</manifest>

提前致谢。

2 个答案:

答案 0 :(得分:1)

您完成了整合广告SDK

的步骤
  1. 元数据标记在那里并且看起来正确
  2. com.google.android.gms.ads.AdActivity已宣布
  3. 您添加了网络和互联网权限
  4. 然后,您似乎调用了广告SDK来加载广告

    // Get the adView.
    AdView adView = (AdView) this.findViewById(R.id.adView);
    
    // Create an ad request. Check logcat output for the hashed device ID to
    // get test ads on a physical device.
    AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)  // Change this if you are using a real device to test
        .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
        .build();
    
    // Start loading the ad in the background.
    adView.loadAd(adRequest);
    

    但你从未真正告诉广告。

    /* This function gets called when the activity is played 
     * and is where we should tell the ad to play too.  
     * For example if your phone goes to sleep then wakes up it will call this function.  
     */
    @Override
    public void onResume() {
        super.onResume();
        if (adView != null) {
            adView.resume();
        }
    }
    
    @Override
    public void onPause() {
        if (adView != null) {
            adView.pause();
        }
        super.onPause();
    }
    

答案 1 :(得分:0)

    <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>

此处您有问题,请将此删除,然后重试。