2014-08-30 171 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。