gpt4 book ai didi

java - 如何在 Fragment 上初始化 Google AdMob 横幅

转载 作者:行者123 更新时间:2023-12-01 21:48:53 26 4
gpt4 key购买 nike

我正在尝试编写代码来初始化 Fragment 上的 Google Admobs 横幅。

我读到代码是

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

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

我认为这段代码是针对Activities的,而不是针对Fragments的

我的问题是如何在片段上初始化 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);
}
}

最佳答案

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

关于java - 如何在 Fragment 上初始化 Google AdMob 横幅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58769617/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com