在课堂上给予错误的adob banner banner广告声明

时间:2014-07-29 14:19:41

标签: android admob banner-ads

在我的安卓游戏中,我在课堂上宣布了admob横幅广告,而不是布局。我在onSetContentView中使用了以下代码:

 protected void onSetContentView() {
        final FrameLayout frameLayout = new FrameLayout(this);
        final FrameLayout.LayoutParams frameLayoutLayoutParams =
                new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
                                             FrameLayout.LayoutParams.FILL_PARENT);

        final AdView adView = new AdView(this, AdSize.BANNER, "ca-app-pub-7554016237953661/7036151237");

        adView.refreshDrawableState();
        adView.setVisibility(AdView.VISIBLE);
        final FrameLayout.LayoutParams adViewLayoutParams =
                new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                                             FrameLayout.LayoutParams.WRAP_CONTENT,
                                             Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM);


        AdRequest adRequest = new AdRequest();
        adView.loadAd(adRequest);

        this.mRenderSurfaceView = new RenderSurfaceView(this);
        mRenderSurfaceView.setRenderer(mEngine);

        final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
                new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());

        frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
        frameLayout.addView(adView, adViewLayoutParams);

        this.setContentView(frameLayout, frameLayoutLayoutParams);

    }

在我使用的清单文件中:

<android:name="com.google.ads.AdActivity"            
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />         

错误日志返回以下错误:

FATAL EXCEPTION: main
java.lang.NoSuchMethodError: android.widget.FrameLayout$LayoutParams.<init>
at com.nguyenxuan.fruitmatching.Menu.onSetContentView(Menu.java:132)
at org.anddev.andengine.ui.activity.BaseGameActivity.onCreate(BaseGameActivity.java:62)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.access$600(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:3)

我认为您使用的是19的构建目标。您的代码将编译,但会在Android&lt;上运行的设备/模拟器上崩溃Api等级19因为,

API中提供了FrameLayout.LayoutParams(FrameLayout.LayoutParams source)构造函数。您应该转换为旧的目标API 1 FrameLayout.LayoutParams (ViewGroup.MarginLayoutParams source)。修改您的代码如下:

 final FrameLayout.LayoutParams frameLayoutLayoutParams =
               ((ViewGroup.MarginLayoutParams) (new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
                                             FrameLayout.LayoutParams.FILL_PARENT)));

 final FrameLayout.LayoutParams adViewLayoutParams =
               ((ViewGroup.MarginLayoutParams) (new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                                             FrameLayout.LayoutParams.WRAP_CONTENT,
                                             Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM)));

答案 1 :(得分:0)

这个问题不是由你的logcat输出中提到的admob引起的。

它说FrameLayout.LayoutParams不是一种方法。无论您身在何处:FrameLayout.LayoutParams将其替换为ViewGroup.LayoutParams,它应该可以解决您的问题。