2014-08-30 121 views
0

我是Android的初学者,我试图在webview上展示广告,但我在布局文件中遇到了问题“以下类无法实例化:com.google.android.gms。 ads.AdView”。我遵循了使用谷歌播放服务所涉及的所有步骤。非常感谢您的帮助。Admob + Android WebView问题

MainActivity.Java

package com.test.webviewcode; 


import com.test.webviewcode.R; 
import com.google.android.gms.ads.*; 
import android.content.res.Configuration; 
import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.widget.LinearLayout; 
import com.google.android.gms.ads.AdRequest; 
import com.google.android.gms.ads.AdSize; 
import com.google.android.gms.ads.AdView; 
import android.app.Activity; 


public class MainActivity extends Activity { 

    private AdView adView; 
    private WebView mWebView; 
    private static final String AD_UNIT_ID = "xxxxxxxxxxxxxxxxxxxxxxxxx"; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mWebView = (WebView) findViewById(R.id.activity_main_webview); 
     // Enable Javascript 
     WebSettings webSettings = mWebView.getSettings(); 
     webSettings.setJavaScriptEnabled(true); 
     mWebView.loadUrl("https://www.google.com/"); 
     // Stop local links and redirects from opening in browser instead of WebView 
     mWebView.setWebViewClient(new WebViewClient1()); 


      // Create an ad. 
      adView = new AdView(this); 
      adView.setAdSize(AdSize.BANNER); 
      adView.setAdUnitId(AD_UNIT_ID); 
      LinearLayout Layout = (LinearLayout) findViewById(R.id.adView); 
      Layout.addView(adView); 
      AdRequest adRequest = new AdRequest.Builder() 
      .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
      .addTestDevice("yyyyyyyyyyyyyyyyyyyyyyyyyyy") 
      .build(); 
      adView.loadAd(adRequest); 

    } 


    @Override 
    public void onBackPressed() { 
     if(mWebView.canGoBack()) { 
      mWebView.goBack(); 
     } else { 
      super.onBackPressed(); 
     } 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, 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. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
    @Override 
    public void onConfigurationChanged(Configuration newConfig){   
     super.onConfigurationChanged(newConfig); 
    } 
     @Override 
     public void onResume() { 
     super.onResume(); 
     if (adView != null) { 
      adView.resume(); 
     } 
     } 

     @Override 
     public void onPause() { 
     if (adView != null) { 
      adView.pause(); 
     } 
     super.onPause(); 
     } 

     /** Called before the activity is destroyed. */ 
     @Override 
     public void onDestroy() { 
     // Destroy the AdView. 
     if (adView != null) { 
      adView.destroy(); 
     } 
     super.onDestroy(); 
     } 
} 

activity_main.xml

<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.android.gms.ads.AdView 
     xmlns:ads="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/adView" 
     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="xxxxxxxxxxxxxxxxxx" > 
     <!-- 
     ads:loadAdOnCreate="true" 
     ads:refreshInterval="30" > 
     --> 
    </com.google.android.gms.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/ProgressBar" 
      style="?android:attr/progressBarStyleHorizontal" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:indeterminate="false" 
      android:progress="50" 
      android:progressDrawable="@drawable/greenprogress" /> 

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


</RelativeLayout> 

Mainfest File

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

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

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
       <meta-data android:name="com.google.android.gms.version" 
       android:value="@integer/google_play_services_version" /> 
     <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="com.google.android.gms.ads.AdActivity" 
     android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/> 


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

</manifest> 
+0

我认为AdMob的 – 2014-08-30 07:32:44

+0

纳文的乌尔使用横幅进阶,能不能请您详细阐述,并告诉我,我该如何解决这个问题 – tattva 2014-08-30 07:37:41

+0

.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .addTestDevice(“yyyyyyyyyyyyyyyyyyyyyyyyyyy”)从删除此两个线路你的代码和没有问题在你的我认为是加载请回你的屏幕我认为你的电子邮件广告 – 2014-08-30 07:42:45

回答

0

这意味着你还没有包括谷歌Play服务库中的APK。

请注意,您的代码还存在其他一些基本问题。

  1. 您正在使用代码和XML创建AdView。不要这样做。只创建一次。我建议用XML。然后使用findViewById来获取对它的引用。
  2. 您的XML中有2个项目,其ID为adView。这将永远不会工作。给LinearLayout一个不同的ID。