2012-06-24 27 views
1

我正在运行Admob 6.0.1并试图将广告添加到表面视图的顶部,我可以说下面的代码在Android 3.2(一个真正的设备),并且在模拟器中使用android 4设备也能正常工作,但是当我尝试在pre api13上进行测试时,例如2.3.3或更低版本时,广告不会显示。现在这里是怪异的部分,如果我改变Surfaceview(在xml中)的可见性,以隐藏广告将显示!这是怎么回事??这只是模拟器是越野车,还是我有一个真正的问题?Admob + Surfaceview打得不好

我试图保持代码尽可能简单,以重现错误。我添加了一个按钮即可显示surfaceview确实允许其他视图更新,只是没有AdMob中查看的...

package com.google.example.ads.fundamentals; 

    import android.app.Activity; 
    import android.content.Context; 
    import android.graphics.Canvas; 
    import android.graphics.Color; 
    import android.os.Bundle; 
    import android.util.AttributeSet; 
    import android.util.Log; 
    import android.view.SurfaceHolder; 
    import android.view.SurfaceView; 

    import com.google.ads.AdView; 

    public class BannerSample extends Activity { 
    /** The view to show the ad. */ 
    private AdView adView; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.layout); 
     adView = (AdView) findViewById(R.id.adView); 
    } 

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

    } 

    class Panel extends SurfaceView implements SurfaceHolder.Callback { 
    private final TutorialThread _thread; 

    public Panel(Context context, AttributeSet att) { 
     super(context, att); 
     getHolder().addCallback(this); 
     _thread = new TutorialThread(getHolder(), this); 

    } 

    @Override 
    public void onDraw(Canvas canvas) { 

     canvas.drawColor(Color.BLUE); 
     Log.d("Hello", "drawing stuff"); 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, 
      int height) { 
     Log.d("HELLO", "surface changed" + holder.getSurfaceFrame().width() 
       + "Height: " + holder.getSurfaceFrame().height()); 

    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     _thread.setRunning(true); 
     _thread.start(); 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     // simply copied from sample application LunarLander: 
     // we have to tell thread to shut down & wait for it to finish, or 
     // else 
     // it might touch the Surface after we return and explode 
     boolean retry = true; 
     _thread.setRunning(false); 
     while (retry) { 
      try { 
       _thread.join(); 
       retry = false; 
      } catch (InterruptedException e) { 
       // we will try it again and again... 
      } 
     } 
    } 
    } 

    class TutorialThread extends Thread { 
    private final SurfaceHolder _surfaceHolder; 
    private final Panel _panel; 
    private boolean _run = false; 

    public TutorialThread(SurfaceHolder surfaceHolder, Panel panel) { 
     _surfaceHolder = surfaceHolder; 
     _panel = panel; 
    } 

    public void setRunning(boolean run) { 
     _run = run; 
    } 

    @Override 
    public void run() { 
     Canvas c; 
     while (_run) { 
      c = null; 
      try { 
       c = _surfaceHolder.lockCanvas(null); 
       synchronized (_surfaceHolder) { 
        _panel.onDraw(c); 
       } 
      } finally { 
       // do this in a finally so that if an exception is thrown 
       // during the above, we don't leave the Surface in an 
       // inconsistent state 
       if (c != null) { 
        _surfaceHolder.unlockCanvasAndPost(c); 
       } 
      } 
     } 
    } 
    } 

和XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 
    <com.google.example.ads.fundamentals.Panel 
     android:id="@+id/mainPanel" android:layout_width="match_parent" 
     android:layout_height="match_parent" android:visibility="invisible" /> 
    <com.google.ads.AdView android:id="@+id/adView" 
     android:layout_width="match_parent" android:layout_height="wrap_content" 
     android:layout_gravity="top" ads:adSize="BANNER"   ads:adUnitId="xxxxxxxxx" 
     ads:loadAdOnCreate="true" ads:testDevices="TEST_EMULATOR" /> 

    <Button android:id="@+id/mybutton" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:layout_below="@id/adView" 
     android:text="hello" /> 
</RelativeLayout> 

而且清单:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="com.google.example.ads.fundamentals" 
      android:versionCode="1" 
      android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="15" /> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".BannerSample" 
           android:screenOrientation="portrait" 
        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.ads.AdActivity" 
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/> 

    </application> 

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <uses-permission android:name="android.permission.INTERNET"/> 
</manifest> 

任何意见或帮助将是如此之大,我已经试过张贴在谷歌网上论坛对AdMob的,但我认为它得到了一个空白失去..

回答

0

尝试将AdView放在SurfaceView之外,而不是最上面。您可以通过将顶部的AdView广告对齐并在其下方设置SurfaceView来完成此操作。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 
    <com.google.example.ads.fundamentals.Panel 
     android:id="@+id/mainPanel" android:layout_width="match_parent" 
     android:layout_height="match_parent" android:visibility="invisible" 
     android:layout_below="@+id/adView" /> 
    <com.google.ads.AdView android:id="@id/adView" 
     android:layout_width="match_parent" android:layout_height="wrap_content" 
     android:layout_gravity="top" ads:adSize="BANNER" ads:adUnitId="xxxxxxxxx" 
     ads:loadAdOnCreate="true" ads:testDevices="TEST_EMULATOR" 
     android:layout_alignParentTop="true" /> 
</RelativeLayout> 
+0

试过了,没有运气对不起。 – ianmere

+0

有没有人得到调解与运行预安卓3.2的SurfaceView工作?如果我使用正常的Admob集成(无中介)广告显示,但是当我将发布者ID交换到中介ID时:广告不会加载,但是如果我然后删除表面视图,它们将再次! 我设法得到这个类似的代码与inmobi,tapfortap,普通admob,但我尝试使用中介,它在前3.2失败的时刻,ics工作正常。 所以我真的认为这是一个中介框架中的错误,我不认为这是简单的,因为我没有与其他类似集成问题 – ianmere

1

事实证明这是模拟器的问题,当代码在真实设备上运行时,它工作正常!疯狂的模拟器...