无法将admob广告横幅添加到我的Fragment扩展课程中

时间:2014-12-20 02:13:47

标签: java xml

interstitial = new InterstitialAd(HomeFragment.this);

上面的代码部分给出了以下错误:

The constructor InterstitialAd(HomeFragment) is undefined

这是我的HomeFragment.java文件:

package info.example.test;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.app.Activity;

@SuppressLint("SetJavaScriptEnabled") public class HomeFragment extend Fragment {

    private InterstitialAd interstitial;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        WebView webView = (WebView)rootView.findViewById(R.id.webView);
        webView.setInitialScale(1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setScrollbarFadingEnabled(false);
        webView.loadUrl("http://www.company.com");

        return rootView;

        // Prepare the Interstitial Ad
        interstitial = new InterstitialAd(HomeFragment.this);
        // Insert the Ad Unit ID
        interstitial.setAdUnitId("ca-app-pub-455255552555555551543028");

        //Locate the Banner Ad in activity_main.xml
        AdView adView = (AdView) this.findViewById(R.id.adView);

        // Request for Ads
        AdRequest adRequest = new AdRequest.Builder()

        // Add a test device to show Test Ads
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice("CC5F2555555555555A198")
        .build();

        // Load ads into Banner Ads
        adView.loadAd(adRequest);

        // Load ads into Interstitial Ads
        interstitial.loadAd(adRequest);

        // Prepare an Interstitial Ad Listener
        interstitial.setAdListener(new AdListener() {
            public void onAdLoaded() {
                // Call displayInterstitial() function
                displayInterstitial();
            }
        });
    }
    private AdView findViewById(int adview) {
        // TODO Auto-generated method stub
        return null;
    }
    public void displayInterstitial() {
        // If Ads are loaded, show Interstitial else show nothing.
        if (interstitial.isLoaded()) {
            interstitial.show();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您正在使用Fragment,这就是为什么您必须使用getContext()而不是this来处理Context。

更改

interstitial = new InterstitialAd(HomeFragment.this);

interstitial = new InterstitialAd(rootView.getContext());

执行此更改并将AdMob代码放入rootView部分。