AdMob in a fragment

房东的猫 提交于 2019-12-30 05:23:07

问题


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" />
    

回答1:


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.




回答2:


You have to write root before the findViewById:

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


来源:https://stackoverflow.com/questions/21512446/admob-in-a-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!