2014-10-09 118 views
0

伙计们我为广告插入了此代码,但在我的手机上进行测试时,它们未显示在我的应用中。在项目中没有错误,一切工作正常,只是添加不显示。这里是我的代码让我知道,如果你想看到activity_main.xml。Admob广告未在应用中显示

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> 
+0

你有没有更换为ca-app-pub- xxx与代码中的adunitid? – Psypher 2014-10-09 20:11:38

回答

0

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

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