android webview admob not displaying

2020-04-01 04:12发布

问题:

i want to use google adds at bottom of my page.. into the emulator it properly displays the add but in device it doesn't
it gives me..

07-20 06:16:24.931: WARN/webcore(4289): Can't get the viewWidth after the first layout
07-20 06:16:25.101: WARN/Ads(4289): Invalid unknown request error: Cannot determine request type. Is your ad unit id correct?
07-20 06:16:25.111: INFO/Ads(4289): onFailedToReceiveAd(Invalid Google Ad request.)
07-20 06:16:28.957: WARN/PowerManagerService(175): Timer 0x7->0x3|0x0
07-20 06:16:28.957: INFO/PowerManagerService(175): Ulight 7->3|0
07-20 06:16:30.351: WARN/InputManagerService(175): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4a43b8f8
07-20 06:16:45.961: WARN/PowerManagerService(175): Timer 0x3->0x1|0x0
07-20 06:16:45.961: INFO/PowerManagerService(175): Ulight 3->1|0

i cant understand what is the problem? the same code is properly running on the emulator but not in device my XML file is

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:id="@+id/rltvLayout"
    android:layout_height="fill_parent">

    <LinearLayout android:id="@+id/linearLayout1"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:orientation="vertical">
        <WebView android:id="@+id/webView" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:fitsSystemWindows="true"/>

        </LinearLayout>
        <LinearLayout android:layout_width="fill_parent"
 android:id="@+id/ad_layout"
 android:layout_height="wrap_content"
 android:gravity="bottom"
 android:layout_alignParentBottom="true"
 android:layout_alignBottom="@+id/home_layout">
 </LinearLayout>
            </RelativeLayout>

回答1:

i've use this in xml file

<?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="fill_parent" android:id="@+id/rltvLayout1"
    android:layout_height="fill_parent">
    <LinearLayout android:id="@+id/linearLayoutwebview"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:orientation="vertical">
        <WebView android:id="@+id/webView1"
            android:layout_width="fill_parent" android:layout_height="fill_parent"
            android:fitsSystemWindows="true" />
    </LinearLayout>
    <LinearLayout android:layout_width="fill_parent"
        android:id="@+id/ad_layout" android:layout_height="wrap_content"
        android:gravity="bottom" android:layout_alignParentBottom="true"
        android:layout_alignBottom="@+id/home_layout">
        <com.google.ads.AdView android:layout_width="wrap_content"
            android:layout_height="wrap_content" ads:adUnitId="here put ID"
            ads:adSize="BANNER" android:id="@+id/adView" ads:refreshInterval="60" />

    </LinearLayout>
</RelativeLayout>

one more point i found is that
There are some changes to the way Admob works in version 4.1.0
1) Remove attrs.xml (or if you need it for your own custom attributes, remove the parts related to AdViews).

2) Change the namespace in your layout from xmlns:ads="http://schemas.android.com/apk/res/com.your.packagename" to xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" see more...



回答2:

Edit the code as below, add these code

    android:layout_above="@+id/ad_layout"
    android:layout_alignParentTop="true"

Your xml should be like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:id="@+id/rltvLayout"
android:layout_height="fill_parent">

<LinearLayout android:id="@+id/linearLayout1"
    android:layout_height="wrap_content" android:layout_width="wrap_content"
    android:orientation="vertical" android:layout_above="@+id/ad_layout"
    android:layout_alignParentTop="true">
    <WebView android:id="@+id/webViewMakeMp3" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:fitsSystemWindows="true"/>

    </LinearLayout>
    <LinearLayout android:layout_width="fill_parent"
      android:id="@+id/ad_layout"
      android:layout_height="wrap_content"
      android:gravity="bottom"
      android:layout_alignParentBottom="true"
      android:layout_alignBottom="@+id/home_layout">
    </LinearLayout>
   </RelativeLayout>