How to use admob in android web browser?

Hi everyone, I am using admob in an Android browser. When I integrate admob and run it, it displays the 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 i code, which is 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 outputs the 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 figure out where I am going wrong.

Thanks in advance

0
android android-layout android-webview admob
source share
2 answers

I assume that you are using admob through the google play services library and not the old / obsolete 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> 

in

 <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
source share

So, Stacktrace says: "Did not find class com.google.ads.AdView"

This is because you are probably using the Admob SDK through Google Play Services, which is required from August 1. (Otherwise, you will not be able to download the application on the Play Store). So instead of com.google.ads.AdView you need to use com.google.android.gms.ads.AdView .

You can read more about transferring the Admob SDK to Google Play on the official website .

Edit : By the way, you need to move the Admob-Banner layout below LinearLayout, otherwise it will not be visible.

+1
source share

All Articles