AdMob在一个片段中 [英] AdMob in a fragment

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

问题描述

我在我的应用程序中插入了AdMob。但我有一个问题。

当我在Eclipse中插入一些关于它的代码时,我可以看到有关构造函数AdRequest()不可见的错误消息in a fragment_03 anderror:Error解析XML:未绑定前缀从

,因此我附加了AdMob的源代码,如下所示。

你能给我一个建议吗?

让我知道您是否有任何问题。



谢谢


  • fragment_03.java

      import com.google.ads。 AdRequest; 
    import com.google.ads.AdView;

    public class Fragment_03 extends Fragment {

    private AdView adView;
    private AdRequest adRequest;

    @Override
    public查看onCreateView(LayoutInflater inflater,ViewGroup容器,
    Bundled savedInstanceState){
    查看rootView = inflater.inflate(R.layout.fragment_03,container,假);

    adRequest = new AdRequest();
    adView =(AdView)rootView.findViewById(R.id.fragment_03 _admob);
    adView.loadAd(adRequest);

    返回rootView;



  • fragment_03.xml

     <?xml version =1.0encoding =utf-8?> 
    < LinearLayout xmlns:android =http://schemas.android.com/apk/res/android
    android:layout_width =match_parent
    android:layout_height =match_parent
    android:orientation =vertical>
    < com.google.android.ads.AdView
    xmlns:ads =schemas.android.com/apk/lib/com.google.ads
    android:id =@ + id / fragment_03_admob
    android:layout_width =match_parent
    android:layout_height =wrap_content
    app:adSize =BANNER
    app:adUnitId =@ string / activity_admobID
    app:loadAdOnCreate =true
    app:testDevices =TEST_EMULATOR,TEST_DEVICE_ID/>

    < / LinearLayout>


  • AndroidManifest.xml

     < uses-permission android:name =android.permission.ACCESS_NETWORK_STATE/> 
    < uses-permission android:name =android.permission.INTERNET/>

    android:name =com.google.ads.AdActivity
    android:configChanges =keyboard | keyboardHidden | orientation | screenLayout | uiMode | screenSize | smallestScreenSize />



解决方案

以这种方式更改布局:

 <?xml version =1.0encoding =utf-8?> ; 
< LinearLayout xmlns:android =http://schemas.android.com/apk/res/android
xmlns:app =schemas.android.com/apk/lib/com.google .ads
android:layout_width =match_parent
android:layout_height =match_parent
android:orientation =vertical>
< com.google.android.ads.AdView
android:id =@ + id / fragment_03_admob
android:layout_width =match_parent
android:layout_height = wrap_content
app:adSize =BANNER
app:adUnitId =@ string / activity_admobID
app:loadAdOnCreate =true
app:testDevices =TEST_EMULATOR,TEST_DEVICE_ID />

< / LinearLayout>

您使用了错误的名称空间声明。


I'm inserting an AdMob in my application. but I have a problem about it.

when I inserted some codes about it in Eclipse, I can see an error message about "the constructor AdRequest() is not visible " in a fragment_03 and "error: Error parsing XML : unbound prefix" from "

so I attached source for the AdMob as follows.

Can you give me an advice ?

Let me know if you have any question.

Thanks

  • fragment_03.java

    import com.google.ads.AdRequest;
    import com.google.ads.AdView;
    
    public class Fragment_03 extends Fragment {
    
    private AdView adView;
    private AdRequest adRequest;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_03, container, false);
    
        adRequest = new AdRequest();
        adView = (AdView) rootView.findViewById(R.id.fragment_03_admob);
        adView.loadAd(adRequest);
    
        return rootView;
        }
    }
    

  • fragment_03.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <com.google.android.ads.AdView
            xmlns:ads="schemas.android.com/apk/lib/com.google.ads"
            android:id="@+id/fragment_03_admob"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:adSize="BANNER"
            app:adUnitId="@string/activity_admobID"
            app:loadAdOnCreate="true"
            app:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" />
    
    </LinearLayout>
    

  • AndroidManifest.xml

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    
    <activity
            android:name="com.google.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
    

解决方案

Change your layout in this way:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <com.google.android.ads.AdView
        android:id="@+id/fragment_03_admob"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:adSize="BANNER"
        app:adUnitId="@string/activity_admobID"
        app:loadAdOnCreate="true"
        app:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" />

</LinearLayout>

You was using a wrong namespace declaration.

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

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