How to use admob in android webview?

泄露秘密 提交于 2019-12-11 22:16:36

问题


Hi all I am using admob in android webview. When i integrated admob and run, it displays following error. My code is as follows:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<com.google.ads.AdView
    android:id="@+id/adViews"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="false"
    android:layout_centerHorizontal="true"
    ads:adSize="BANNER"
    ads:adUnitId="ca-123456" >
</com.google.ads.AdView>

<LinearLayout
    android:layout_alignParentTop="true"
    android:layout_above="@id/adView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
   />

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

in code i used as:

 private InterstitialAd interstitial;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    overridePendingTransition(R.anim.trans_left_in, R.anim.trans_left_out);
    setContentView(R.layout.activity_news);


     interstitial = new InterstitialAd(News.this);
        // Insert the Ad Unit ID
        interstitial.setAdUnitId("ca-app123456");

        //Locate the Banner Ad in activity_main.xml
        AdView adView = (AdView) this.findViewById(R.id.adViews);

        // Request for Ads
        AdRequest adRequest = new AdRequest.Builder()

        // Add a test device to show Test Ads..comment for real app when launching
         .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
         .addTestDevice("CC5F2C72DF2B356BBF0DA198")
                .build();

        // Load ads into Banner Ads
        adView.loadAd(adRequest);

        // Load ads into Interstitial Ads
        interstitial.loadAd(adRequest);

        // Prepare an Interstitial Ad Listener
        interstitial.setAdListener(new AdListener() {
            public void onAdLoaded() {
                // Call displayInterstitial() function
                displayInterstitial();
            }
        });
    }
    public void displayInterstitial() {
        // If Ads are loaded, show Interstitial else show nothing.
        if (interstitial.isLoaded()) {
            interstitial.show();
        }

    web=(WebView)findViewById(R.id.webView1);

    web.setWebViewClient(new WebViewClient());
    pb=(ProgressBar)findViewById(R.id.progressBar1);
    ActionBar ab=getActionBar();
    ColorDrawable cd=new ColorDrawable(Color.parseColor("#000000"));
    ab.setBackgroundDrawable(cd);
    ab.setDisplayHomeAsUpEnabled(true);

    WebSettings ws=web.getSettings();
    ws.setBuiltInZoomControls(true);
    web.setWebViewClient(new WebViewClient());

    web.setWebChromeClient(new WebChromeClient() {
      //showing webview
     }
     }

When i run the code it displays following error:

08-10 13:23:50.241: E/AndroidRuntime(13873): FATAL EXCEPTION: main
08-10 13:23:50.241: E/AndroidRuntime(13873): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.all.news/com.all.news.News}: android.view.InflateException: Binary XML file line #7: Error inflating class com.google.ads.AdView
08-10 13:23:50.241: E/AndroidRuntime(13873):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at android.app.ActivityThread.access$600(ActivityThread.java:156)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at android.os.Looper.loop(Looper.java:153)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at android.app.ActivityThread.main(ActivityThread.java:5297)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at java.lang.reflect.Method.invokeNative(Native Method)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at java.lang.reflect.Method.invoke(Method.java:511)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at dalvik.system.NativeStart.main(Native Method)
08-10 13:23:50.241: E/AndroidRuntime(13873): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.google.ads.AdView
08-10 13:23:50.241: E/AndroidRuntime(13873):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:274)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at android.app.Activity.setContentView(Activity.java:1881)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at com.all.news.News.onCreate(News.java:35)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at android.app.Activity.performCreate(Activity.java:5122)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
08-10 13:23:50.241: E/AndroidRuntime(13873):    ... 11 more
08-10 13:23:50.241: E/AndroidRuntime(13873): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.ads.AdView" on path: DexPathList[dexElements=[zip file "/data/app/com.all.news-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.all.news-1, /vendor/lib, /system/lib]]
08-10 13:23:50.241: E/AndroidRuntime(13873):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at android.view.LayoutInflater.createView(LayoutInflater.java:552)
08-10 13:23:50.241: E/AndroidRuntime(13873):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
08-10 13:23:50.241: E/AndroidRuntime(13873):    ... 21 more

Please help me to sort where I am doing mistake..

Thanks in advance


回答1:


I suppose you're using admob via the google play service library and not the old/deprecated admob sdk jar, so in your layout, change :

<com.google.ads.AdView
    android:id="@+id/adViews"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="false"
    android:layout_centerHorizontal="true"
    ads:adSize="BANNER"
    ads:adUnitId="ca-123456" >
</com.google.ads.AdView>

to:

<com.google.android.gms.ads.AdView
    android:id="@+id/adViews"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="false"
    android:layout_centerHorizontal="true"
    ads:adSize="BANNER"
    ads:adUnitId="ca-123456"/>



回答2:


So the Stacktrace says "Didn't find class com.google.ads.AdView"

That's because you're probably using the Admob SDK via the Google Play Services which is mandatory as of 1st of August. (Otherwise you won't be able to upload an application to the Play Store). So instead of using com.google.ads.AdView you need to use com.google.android.gms.ads.AdView.

You can read more about the Migration of the Admob SDK to the Google Play Services on the official developer site (click)

Edit: By the way, you have to move the Admob-Banner layout-code below the LinearLayout otherwise it won't be visible.



来源:https://stackoverflow.com/questions/25226698/how-to-use-admob-in-android-webview

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