Admob的在片段 [英] Admob in fragments

查看:133
本文介绍了Admob的在片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了appcompact一个空项目,并试图给它添加了AdMob的块

I created an empty project with appcompact and tried to add to it the AdMob block

fragment_main.xml

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>

MainActivity.java

MainActivity.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;

        }
    }

在行AD浏览报=新的AD浏览报(getActivity());错误无法到达code

In line "adView = new AdView(getActivity());" error "Unreachable code"

推荐答案

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 返回细

这篇关于Admob的在片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆