gpt4 book ai didi

java - fragment 中的 Admob

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:48:16 24 4
gpt4 key购买 nike

我用 appcompact 创建了一个空项目并尝试向其中添加 AdMob block

fragment _main.xml

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

tools:context="com.example.testadmobv.MainActivity$PlaceholderFragment" >
<LinearLayout
android:id="@+id/layout_admob"

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

</RelativeLayout>

主 Activity .java

public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;

adView = new AdView(getActivity());
adView.setAdUnitId("MY_AD_UNIT_ID");
adView.setAdSize(AdSize.BANNER);
LinearLayout layout = (LinearLayout)rootView.findViewById(R.id.layout_admob);
layout.addView(adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
return rootView;

}
}

在行“adView = new AdView(getActivity());”错误“无法访问的代码”

最佳答案

你有

View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView; // remove this
adView = new AdView(getActivity()); // this is unreachable coz you have return above.

onCreateView最后的return就可以了

关于java - fragment 中的 Admob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23765132/

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