如何在Fragment上初始化Google AdMob标语

我正在尝试编写代码以初始化片段上的Google Admobs标语。

我已经读过代码是

AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

但是当我在Fragment上编写此代码时,Android Studio无法识别findViewById

我认为这段代码是针对活动的,而不是针对片段的

我的问题是如何在片段上初始化Admob横幅

package com.flixarts.ar.englishnow;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import static com.google.android.gms.ads.AdView.*;

/**
 * A simple {@link Fragment} subclass.
 * activities that contain this fragment must implement the
 * {@link adjectivesfragment1.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link adjectivesfragment1#newInstance} factory method to
 * create an instance of this fragment.
 */
public class adjectivesfragment1 extends Fragment {
    // TODO: Rename parameter arguments,choose names that match
    // the fragment initialization parameters,e.g. ARG_ITEM_NUMber
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public adjectivesfragment1() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment adjectivesfragment1.
     */
    // TODO: Rename and change types and number of parameters

    public static adjectivesfragment1 newInstance(String param1,String param2) {
        adjectivesfragment1 fragment = new adjectivesfragment1();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1,param1);
        args.putString(ARG_PARAM2,param2);
        fragment.setarguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getarguments() != null) {
            mParam1 = getarguments().getString(ARG_PARAM1);
            mParam2 = getarguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_adjectivesfragment1,container,false);
    }

    // TODO: Rename method,update argument and hook method into UI event
    public void onButtonpressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
maodengsan0000 回答:如何在Fragment上初始化Google AdMob标语

在片段中使用getActivity()。findViewById()。

本文链接:https://www.f2er.com/3136806.html

大家都在问