Admob + Android WebView问题

时间:2014-08-30 07:12:53

标签: android webview admob adview

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

  

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文件

<?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>

1 个答案:

答案 0 :(得分:0)

这意味着您尚未在自己的APK中添加Google Play服务库。

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

  1. 您正在使用代码和XML创建AdView。不要那样做。仅创建一次。我建议使用XML。然后使用findViewById获取对它的引用。
  2. 您的XML中有2个ID为adView的项目。那永远不会奏效。为LinearLayout提供不同的ID。