让admob和webview一起工作

时间:2014-03-04 21:52:43

标签: android eclipse admob

我已经构建了一个运行良好的webview应用程序。

我尝试在应用程序的底部添加admob,但它没有显示。

我已经弄清楚我是否将主要活动设置为

 setContentView(R.layout.activity_main);

我收到广告,但没有网络视图

如果我将主要活动设置为

setContentView(mWebview );

我收到了网页视图,但没有广告。

我怎样才能让两者一起展示?


mainActivity.java

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {


    private WebView mWebview ;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        super.onCreate(savedInstanceState);

        mWebview = new WebView(this);

        mWebview.getSettings().setJavaScriptEnabled(true); 

        final Activity activity = this;

        mWebview.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
            }

        });
        mWebview .loadUrl("file:///android_asset/app/index.html");
        //setContentView(mWebview);


    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(event.getAction() == KeyEvent.ACTION_DOWN){
            switch(keyCode)
            {
            case KeyEvent.KEYCODE_BACK:
                if(mWebview.canGoBack() == true){
                    mWebview.goBack();
                }else{
                    finish();
                }
                return true;
            }

        }
        return super.onKeyDown(keyCode, event);
    }

}

activity_main.xml中

    <LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

            <com.google.ads.AdView
            android:id="@+id/adView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            ads:adSize="BANNER"
            ads:adUnitId="MYIDMYIDMYID"
            ads:loadAdOnCreate="true"

             >
        </com.google.ads.AdView>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

指向webView1 ex的指针:  mWebview =(WebView)findViewById(R.id.webView1);

试试这个:

MainActivity.java

public class MainActivity extends Activity {
private WebView mWebview ;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //this will create a pointer to the webView1
    mWebview = (WebView)findViewById(R.id.webView1);

    mWebview.getSettings().setJavaScriptEnabled(true); 

    final Activity activity = this;

    mWebview.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
        }

    });
    mWebview .loadUrl("file:///android_asset/app/index.html");



}



@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(event.getAction() == KeyEvent.ACTION_DOWN){
        switch(keyCode)
        {
        case KeyEvent.KEYCODE_BACK:
            if(mWebview.canGoBack() == true){
                mWebview.goBack();
            }else{
                finish();
            }
            return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}}

main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"

tools:context=".MainActivity" >

<com.google.ads.AdView
    android:id="@+id/adView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    ads:adSize="BANNER"
    ads:adUnitId="MYIDMYIDMYID"
    ads:loadAdOnCreate="true" >

</com.google.ads.AdView>

<WebView
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

</WebView>