【问题标题】:AdMob and WebView within the same activity not working (Android app)同一活动中的 AdMob 和 WebView 不起作用(Android 应用)
【发布时间】:2014-12-01 13:10:39
【问题描述】:

我无法让 admob 在 xml 文件中有 webview 标签的活动中工作,横幅广告仅出现 25% 的时间,当它不出现时,我在日志中收到此消息:“广告不可见。不刷新广告”。这是我的代码

XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">

<WebView
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="70dp"
     />

<LinearLayout
 android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:layout_marginBottom="5dp" >

<com.google.android.gms.ads.AdView android:id="@+id/adView"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   ads:adSize="BANNER"
                   ads:adUnitId="MY_ID_UNIT"/>

     </LinearLayout>

  </RelativeLayout>

JAVA 文件

package com.example.quickfeed;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import android.support.v7.app.ActionBarActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;



public class UltimasNoticias extends ActionBarActivity {

 ProgressDialog mProgress;

 private static final String TEST_DEVICE_ID = "MY_ID_UNIT";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ultimas_noticias);
    Intent intent = getIntent();
    WebView myWebView = (WebView) findViewById(R.id.webview);

    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mProgress = ProgressDialog.show(this, "Cargando", "Por favor espere un momento...");



 // The "loadAdOnCreate" and "testDevices" XML attributes no longer available.
    AdView adView = (AdView) this.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice(TEST_DEVICE_ID)
        .build();
    adView.loadAd(adRequest);


    myWebView.setWebViewClient(new WebViewClient() {
        // load url
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        // when finish loading page
        public void onPageFinished(WebView view, String url) {
            if(mProgress.isShowing()) {
                mProgress.dismiss();
            }
        }
    });
    // set url for webview to load
    myWebView.loadUrl("http://www.quickfeed.com.ar/app/ultimas.php");
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.ultimas_noticias, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
WebView myWebView = (WebView) findViewById(R.id.webview);

    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mProgress = ProgressDialog.show(this, "Cargando", "Por favor espere un momento...");


    myWebView.setWebViewClient(new WebViewClient() {
        // load url
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        // when finish loading page
        public void onPageFinished(WebView view, String url) {
            if(mProgress.isShowing()) {
                mProgress.dismiss();
            }
        }
    });
    // set url for webview to load
    myWebView.loadUrl("http://www.quickfeed.com.ar/app/ultimas.php");
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

这是清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.quickfeed"
android:versionCode="1"
android:versionName="1.0" >



<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/CustomActionBarTheme" >



    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".UltimasNoticias"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:label="@string/title_activity_ultimas_noticias"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.quickfeed.MainActivity" />
    </activity>


      <activity android:name="com.google.android.gms.ads.AdActivity"
              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
<meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version" />
</application>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

</manifest>

日志猫:

12-02 07:36:47.476:I/ActivityManager(2370):从 pid 4004 开始 u0 {cmp=com.example.quickfeed/.UltimasNoticias} 显示 0 12-02 07:36:47.482: I/wm_task_moved(2370): [2,1,0] 12-02 07:36:47.484: I/am_create_activity(2370): [0,920230720,2,com.example.quickfeed/.UltimasNoticias,NULL,NULL,NULL,0] 12-02 07:36:47.492: I/am_pause_activity(2370): [0,630334866,com.example.quickfeed/.MainActivity] 12-02 07:36:47.507:E/NetlinkEvent(1640):NetlinkEvent::FindParam():未找到参数“TIME_NS” 12-02 07:36:47.539: I/am_on_paused_call (4004): [0,com.example.quickfeed.MainActivity] 12-02 07:36:47.554: I/am_restart_activity(2370): [0,920230720,2,com.example.quickfeed/.UltimasNoticias] 12-02 07:36:47.572: W/AudioTrack(2370): AUDIO_OUTPUT_FLAG_FAST 被客户端拒绝 12-02 07:36:48.053:W/Resources(4004):预加载的可绘制资源 #0x1080093 (android:drawable/sym_def_app_icon),随配置而异!! 12-02 07:36:48.115: W/art(4004): 尝试从 IRT 中删除本地句柄范围条目,忽略 12-02 07:36:48.503:W/GooglePlayServicesUtil(4004):Google Play 服务已过期。需要 6171000 但找到 5053030 12-02 07:36:48.541:I/Ads(4004):开始广告请求。 12-02 07:36:48.578: I/Ads(4004): 请将 AdActivity 的主题设置为 @android:style/Theme.Translucent 以启用透明背景插页式广告。 12-02 07:36:48.688: I/am_on_resume_called(4004): [0,com.example.quickfeed.UltimasNoticias] 12-02 07:36:48.758:I/Choreographer(4004):跳过 31 帧!应用程序可能在其主线程上做了太多工作。 12-02 07:36:49.069:W/GooglePlayServicesUtil(4004):Google Play 服务已过期。需要 6171000 但找到 5053030 12-02 07:36:49.160:E/GooglePlayServicesUtil(4004):由于错误 2,GooglePlayServices 不可用 12-02 07:36:50.475:I/Choreographer(4004):跳过 163 帧!应用程序可能在其主线程上做了太多工作。 12-02 07:36:51.499: I/art(2805): 背景粘性并发标记扫描 GC 释放 7040(932KB) AllocSpace 对象,0(0B) LOS 对象,485% 空闲,1963KB/6MB,暂停 5.136ms 总计 198.503小姐 12-02 07:36:52.883: W/art(4004): 尝试从 IRT 中删除本地句柄范围条目,忽略 12-02 07:36:53.480:I/Choreographer(4004):跳过 270 帧!应用程序可能在其主线程上做了太多工作。 12-02 07:36:53.708:I/Choreographer(2370):跳过 106 帧!应用程序可能在其主线程上做了太多工作。 12-02 07:36:53.770: I/am_activity_launch_time(2370): [0,920230720,com.example.quickfeed/.UltimasNoticias,6224,6224] 12-02 07:36:53.771: I/ActivityManager(2370): 显示 com.example.quickfeed/.UltimasNoticias: +6s224ms 12-02 07:36:54.223:I/Choreographer(2370):跳过 43 帧!应用程序可能在其主线程上做了太多工作。 12-02 07:36:54.908:I/Choreographer(2370):跳过 63 帧!应用程序可能在其主线程上做了太多工作。 12-02 07:36:54.939:I/Choreographer(4004):跳过 133 帧!应用程序可能在其主线程上做了太多工作。 12-02 07:36:55.482: I/sf_frame_dur(54): [com.example.quickfeed/com.example.quickfeed.MainActivity,0,0,0,1,0,0,1] 12-02 07:36:57.204:I/Choreographer(4004):跳过 68 帧!应用程序可能在其主线程上做了太多工作。 12-02 07:36:57.243: I/chromium(4004): [INFO:CONSOLE(0)] “文档已从应用程序缓存加载,清单 https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.appcache”,来源:https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.html (0) 12-02 07:36:57.278: I/chromium(4004): [INFO:CONSOLE(0)] “应用程序缓存检查事件”,来源:https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.html (0) 12-02 07:36:57.279: I/chromium(4004): [INFO:CONSOLE(0)] “应用程序缓存检查事件”,来源:https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.html (0) 12-02 07:36:57.400: I/chromium(4004): [INFO:CONSOLE(0)] “应用程序缓存 NoUpdate 事件”,来源:https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.html (0) 12-02 07:36:57.405: I/chromium(4004): [INFO:CONSOLE(0)] “应用程序缓存 NoUpdate 事件”,来源:https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.html (0) 12-02 07:36:57.501:W/ActivityManager(2370):启动超时已过期,放弃唤醒锁! 12-02 07:37:00.037:W/Ads(4004):获得广告响应时出现问题。错误代码:0 12-02 07:37:00.362: W/art(4004): 尝试从 IRT 中删除本地句柄范围条目,忽略 12-02 07:37:00.472:W/Ads(4004):无法加载广告:0 12-02 07:37:00.474:I/Choreographer(4004):跳过 30 帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:05.557: E/NetlinkEvent(1640): NetlinkEvent::FindParam(): 参数 'TIME_NS' 未找到 12-02 07:37:05.570: E/NetlinkEvent(1640): NetlinkEvent::FindParam(): 未找到参数“TIME_NS” 12-02 07:37:07.948: I/art(4004): 背景粘性并发标记扫描 GC 释放 20322(1273KB) AllocSpace 对象,6(96KB) LOS 对象,288% 空闲,5MB/9MB,暂停 62.254ms 总计 611.250小姐 12-02 07:37:17.266: E/NetlinkEvent(1640): NetlinkEvent::FindParam(): 参数 'TIME_NS' 未找到 12-02 07:37:17.280: E/NetlinkEvent(1640): NetlinkEvent::FindParam(): 参数 'TIME_NS' 未找到 12-02 07:37:21.021:I/Choreographer(4004):跳过 38 帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:21.402:I/Choreographer(4004):跳过 36 帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:22.265:I/Choreographer(4004):跳过 30 帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:22.299: I/netstats_mobile_sample(2370): [9036180,9261,552404,5716,8888646,9257,472268,5712,8741567,9158,471901,5689,1417523842185] 12-02 07:37:22.304: I/netstats_wifi_sample(2370): [0,0,0,0,0,0,0,0,0,0,0,0,1417523842185] 12-02 07:37:22.754:I/Choreographer(4004):跳过 45 帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:23.079:I/Choreographer(4004):跳过 30 帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:23.418:I/Choreographer(4004):跳过 32 帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:23.750:I/Choreographer(4004):跳过 31 帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:25.048:I/Choreographer(4004):跳过 36 帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:25.950:I/Choreographer(4004):跳过 31 帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:40.975:D/LightsService(2370):延迟设置灯:591ms 12-02 07:37:41.069:D/LightsService(2370):过度延迟设置灯:94 毫秒 12-02 07:37:42.948:I/Choreographer(4004):跳过 149 帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:44.627: I/free_storage_changed(2370): 110465024 12-02 07:37:45.174: I/auditd(3196): type=1400 audit(0.0:24): avc: denied { 0x10 } for comm="Binder_A" 能力=36 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=capability2 12-02 07:37:45.339:I/Choreographer(4004):跳过 97 帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:46.001: I/sf_frame_dur(54): [Cargando,0,0,0,196,83,9,3]

【问题讨论】:

  • 请发布清单文件是否已在清单中添加了权限
  • 是的,我添加了权限,将清单添加到问题中
  • 发布您的 logcat 以了解广告未显示的时间 - 答案将在其中。
  • 以上是未显示广告时的日志,我现在得到一个错误代码 0,但有时它只是“广告不可见”
  • 有什么想法吗??在 logcat 中看不到任何有用的东西

标签: java android webview admob


【解决方案1】:

你的活动布局很奇怪!删除 LinearLayout 并在 com.google.android.gms.ads.AdView android:id="@+id/adView"

中使用以下标签
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"

然后尝试不在 webview 中加载 url。在这种情况下,广告是否会 100% 展示?

【讨论】:

  • 是的,如果我不使用 webview 广告显示 100% 的时间,但使用 webview 标签广告显示大约 25% 的时间。我尝试删除线性布局,但我收到此错误“获取广告响应时出现问题。错误代码:0”
猜你喜欢
  • 1970-01-01
  • 2016-10-29
  • 2015-07-07
  • 1970-01-01
  • 2016-02-26
  • 2015-02-20
  • 1970-01-01
  • 2018-12-07
  • 1970-01-01
相关资源
最近更新 更多