具有相同布局的 WebView 的 AdMob 广告?
Posted
技术标签:
【中文标题】具有相同布局的 WebView 的 AdMob 广告?【英文标题】:AdMob ads with a WebView in the same layout? 【发布时间】:2014-06-18 13:28:39 【问题描述】:我正在尝试使用 WebView 实施 AdMob 广告。对于我的一生,我无法展示任何东西,即使在日志中也是如此。几乎就像根本没有调用广告一样 - 我的 AdMob 帐户没有显示任何请求。起初我认为 WebView 只是占用了布局中的所有空间,但现在我又开始怀疑自己了,因为我没有看到任何与广告相关的 LogCat 输出。
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:layout_
android:layout_>
<com.google.ads.AdView android:id="@+id/adView"
android:layout_
android:layout_
ads:adUnitId="[myIDishere]"
ads:adSize="BANNER"
ads:testDevices="[myhashishere]"
ads:loadAdOnCreate="true"/>
<WebView
android:id="@+id/webView"
android:layout_
android:layout_ />
</LinearLayout>
然后是我的主要活动:
public class MainActivity extends Activity
//StartServicesCheck
private boolean checkPlayServices()
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (status != ConnectionResult.SUCCESS)
if (GooglePlayServicesUtil.isUserRecoverableError(status))
showErrorDialog(status);
else
Toast.makeText(this, "This device is not supported.",
Toast.LENGTH_LONG).show();
finish();
return false;
return true;
void showErrorDialog(int code)
GooglePlayServicesUtil.getErrorDialog(code, this,
REQUEST_CODE_RECOVER_PLAY_SERVICES).show();
static final int REQUEST_CODE_RECOVER_PLAY_SERVICES = 1001;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
switch (requestCode)
case REQUEST_CODE_RECOVER_PLAY_SERVICES:
if (resultCode == RESULT_CANCELED)
Toast.makeText(this, "Google Play Services must be installed.",
Toast.LENGTH_SHORT).show();
finish();
return;
super.onActivityResult(requestCode, resultCode, data);
//EndServicesCheck
private WebView mWebview ;
@Override
public void onBackPressed()
if(mWebview.canGoBack() == true)
mWebview.goBack();
else
super.onBackPressed();
public class BannerExample extends Activity
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Look up the AdView as a resource and load a request.
AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setjavascriptEnabled(true); // enable javascript
mWebview.getSettings().setBuiltInZoomControls(true);
if(android.os.Build.VERSION.SDK_INT >= 11)
mWebview.getSettings().setDisplayZoomControls(false);
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient()
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
);
mWebview .loadUrl("[webpagehere]");
setContentView(mWebview );
mWebview.setWebViewClient(new WebViewClient()
@Override
public void onPageFinished(WebView view, String url)
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
);
我的编码本身有问题吗?我有点倾向于这种方式,因为我没有看到请求或日志输出。
【问题讨论】:
等一下,你的代码搞砸了。您已经在 MainActivity 中定义了 Banner 活动?只创建一个活动.. 【参考方案1】:将WebView
更改为以下内容。你的AdView
被WebView
隐藏了:
<WebView
android:id="@+id/webView"
android:layout_
android:layout_
android:weight="1" />
我还建议将 AdView
的宽度设置为 match_parent 和 adSize
为 "SMART_BANNER"
SMART_BANNER 广告会根据设备的方向和宽度自动调整大小。
编辑
我刚刚浏览了您的代码。它有很多小错误。将其更改为以下内容:
public class MainActivity extends Activity
//StartServicesCheck
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
mWebview = (WebView) findViewById(R.id.webview);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.getSettings().setBuiltInZoomControls(true);
if(android.os.Build.VERSION.SDK_INT >= 11)
mWebview.getSettings().setDisplayZoomControls(false);
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient()
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
);
mWebview .loadUrl("[webpagehere]");
setContentView(mWebview );
mWebview.setWebViewClient(new WebViewClient()
@Override
public void onPageFinished(WebView view, String url)
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
);
private boolean checkPlayServices()
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (status != ConnectionResult.SUCCESS)
if (GooglePlayServicesUtil.isUserRecoverableError(status))
showErrorDialog(status);
else
Toast.makeText(this, "This device is not supported.",
Toast.LENGTH_LONG).show();
finish();
return false;
return true;
void showErrorDialog(int code)
GooglePlayServicesUtil.getErrorDialog(code, this,
REQUEST_CODE_RECOVER_PLAY_SERVICES).show();
static final int REQUEST_CODE_RECOVER_PLAY_SERVICES = 1001;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
switch (requestCode)
case REQUEST_CODE_RECOVER_PLAY_SERVICES:
if (resultCode == RESULT_CANCELED)
Toast.makeText(this, "Google Play Services must be installed.",
Toast.LENGTH_SHORT).show();
finish();
return;
super.onActivityResult(requestCode, resultCode, data);
//EndServicesCheck
private WebView mWebview ;
@Override
public void onBackPressed()
if(mWebview.canGoBack() == true)
mWebview.goBack();
else
super.onBackPressed();
PS:对不起,缩进搞砸了:P
【讨论】:
我刚刚进行了所有这些更改,但我仍然没有看到它:( 在 logcat 我仍然没有看到任何提及尝试加载的广告.. :( 感谢您的帮助,非常感谢。因此,我实施了您的更改,应用程序立即崩溃。 Logcat 声明:06-18 11:36:54.380: E/AndroidRuntime(27310): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.ads.AdView" on path: DexPathList[[zip file "/data/app/com.iamthesquidward.ferris-14.apk"],nativeLibraryDirectories=[/data/app-lib/com.iamthesquidward.ferris-14, /vendor/lib, /system/lib]]
。我假设它找不到任何 AdMob 引用,即使我尝试将 jar 复制到库并引用 Play Services SDK
确保您已正确添加 Google Play 服务,并将您的 adview 替换为 Play 服务中的一个,即 com.google.android.gms.ads.AdView。检查此链接:developers.google.com/mobile-ads-sdk/docs/admob/fundamentals以上是关于具有相同布局的 WebView 的 AdMob 广告?的主要内容,如果未能解决你的问题,请参考以下文章