admob banner ad declaration in class giving errors in android

空扰寡人 提交于 2019-12-13 16:02:09

问题


In My android game I'm declared admob banner ad in class instead of layout. I had used the following code in 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);

    }

And in my manifest file i had used:

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

Error log returns the following error:

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)

回答1:


I suppose you're using a build target of 19. Your code will compile but will crash on devices/emulators running on android < Api Level 19 because,

the FrameLayout.LayoutParams(FrameLayout.LayoutParams source) constructor is available from API 19. You should cast to the older target API 1 FrameLayout.LayoutParams (ViewGroup.MarginLayoutParams source). Modify your code as follows:

 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)));



回答2:


This problem is not caused by admob as is mentioned in your logcat output.

It says FrameLayout.LayoutParams is not a method. Wherever you have: FrameLayout.LayoutParams replace it with ViewGroup.LayoutParams and it should solve your problem.



来源:https://stackoverflow.com/questions/25017680/admob-banner-ad-declaration-in-class-giving-errors-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!