Admob广告未在应用中展示

时间:2014-10-09 13:38:19

标签: android eclipse admob

伙计我已经为我的广告插入了此代码,但是当我在手机上测试时,它们不会显示在我的应用中。项目中没有错误一切正常,只是添加没有显示。以下是我的代码,如果您想查看activity_main.xml,请告诉我。

protected void onCreate(Bundle savedInstanceState) {

    AppUtils.prepareLoadingDialog(MainActivity.this);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    initializeComponents();
    initializeListeners();
    setViewPagerAdapter();

     adView = new AdView(this);
        adView.setAdSize(AdSize.SMART_BANNER);
        adView.setAdUnitId("ca-app-pub-xxx");


        RelativeLayout layout = (RelativeLayout) findViewById(R.id.newAdView);
        layout.addView(adView);


        AdRequest adRequest = new AdRequest.Builder()

            .build();

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

        interstitial = new InterstitialAd(this);
        interstitial.setAdUnitId("ca-app-pub-xxx");

        AdRequest adRequest1 = new AdRequest.Builder().build();
        interstitial.loadAd(adRequest);
        interstitial.setAdListener(new AdListener() {
            public void onAdLoaded() {
                displayInterstitial();
            }

            private void displayInterstitial() {
                if (interstitial.isLoaded()) {
                    interstitial.show();
                }
            }
        });



}

private void initializeComponents(){
    viewPager = (ViewPager)findViewById(R.id.viewPager);
    leftButton = (Button)findViewById(R.id.leftButton);
    rightButton = (Button)findViewById(R.id.rightButton);
    assignWallpaperButton = (Button)findViewById(R.id.assignWallpaperButton);
}

@SuppressLint("HandlerLeak")
private void initializeListeners(){
    leftButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.debug("MainActivity :: left button is clicked");
            int currentItem = viewPager.getCurrentItem();

            viewPager.setCurrentItem(--currentItem);
        }
    });

    rightButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.debug("MainActivity :: right button is clicked");
            int currentItem = viewPager.getCurrentItem();
            viewPager.setCurrentItem(++currentItem);
        }
    });

    assignWallpaperButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.debug("MainActivity :: assign wallpaper button is clicked");
            try {
                int currentItem = viewPager.getCurrentItem();
                WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
                myWallpaperManager.setBitmap(ResourceManager.getWallpaperImage(MainActivity.this, currentItem));
            } catch (IOException e) {
                Log.debug("MainActivity :: "+e.toString());
                Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_LONG).show();
            }
        }
    });

    leftButton.setOnTouchListener(new ButtonTouchListener(leftButton));
    rightButton.setOnTouchListener(new ButtonTouchListener(rightButton));
}

private void setViewPagerAdapter(){
    ImagePagerAdapter adapter = new ImagePagerAdapter(MainActivity.this);
    viewPager.setAdapter(adapter);
}

}

这是我的activity_main.xml

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@drawable/background"
   tools:context=".MainActivity" >

   <RelativeLayout
    android:id="@+id/newAdView"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_marginBottom="50dp"
    android:layout_alignParentBottom="true"
    android:gravity="center_horizontal"/>

  <include
    android:id="@+id/headerLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/margin_medium"
    layout="@layout/header" />

  <RelativeLayout
    android:id="@+id/imageLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/assignWallpaperButton"
    android:layout_below="@+id/headerLayout"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="@dimen/button_arrow_width"
    android:layout_marginRight="@dimen/button_arrow_width"
    android:layout_marginTop="1dp"
    android:layout_marginBottom="60dp"
    android:background="@android:color/transparent" >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/transparent"
        android:layout_alignParentBottom="true" >
    </android.support.v4.view.ViewPager>
   </RelativeLayout>


  <Button
    android:id="@+id/assignWallpaperButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerVertical="true"

    android:background="@color/red_color"

    android:text="@string/assingn_wallpaper"
    android:textColor="@color/white_color"
    android:textSize="@dimen/button_text_size"
    android:textStyle="bold" />

  <Button
    android:id="@+id/leftButton"
    android:layout_width="@dimen/button_arrow_width"
    android:layout_height="@dimen/button_arrow_height"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:background="@drawable/arrow_left" />

  <Button
    android:id="@+id/rightButton"
    android:layout_width="@dimen/button_arrow_width"
    android:layout_height="@dimen/button_arrow_height"
    android:layout_alignBaseline="@+id/leftButton"
    android:layout_alignBottom="@+id/leftButton"
    android:layout_alignParentRight="true"
    android:background="@drawable/arrow_right" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

广告可能并不总是可用,您应该use test ads

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