Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将admob广告横幅添加到我的片段扩展类时出现问题_Java_Xml - Fatal编程技术网

Java 将admob广告横幅添加到我的片段扩展类时出现问题

Java 将admob广告横幅添加到我的片段扩展类时出现问题,java,xml,Java,Xml,代码的上述部分给出了以下错误: interstitial = new InterstitialAd(HomeFragment.this); 这是我的HomeFragment.java文件: The constructor InterstitialAd(HomeFragment) is undefined 您正在使用Fragment,这就是为什么您必须使用getContext()而不是this来处理上下文 改变 package info.example.test; import com.goo

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

interstitial = new InterstitialAd(HomeFragment.this);
这是我的
HomeFragment.java
文件:

The constructor InterstitialAd(HomeFragment) is undefined

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

改变

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();
        }
    }
}

因此,进行此更改并将AdMob代码放入
rootView
部分

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