实施AdMob以测试我的应用

时间:2014-09-12 14:59:58

标签: android admob google-play-services

我正在尝试在我的应用上测试adMob。我按照https://developers.google.com/mobile-ads-sdk/docs/admob/android/quick-start中的步骤进行操作,除了这部分外,一切看起来都很好:

它在网站上说:“在RelativeLayout中添加PlaceHolder片段,并在PlaceHolder片段下面包含AdFragment:”

<fragment   android:name="com.google.android.gms.example.bannerexample.MyActivity$PlaceholderFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/adFragment" />

<fragment
            android:id="@+id/adFragment"
            android:name="com.google.android.gms.example.bannerexample.MyActivity$AdFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" />

当我将这个部分添加到我的xml时,下面的这部分有一个红色,它说“无法解析符号”

.example.bannerexample.MyActivity$PlaceholderFragment

没有解释,我想我必须根据我的应用程序改变这一行的某些部分,但哪一部分?

还是我遗漏的其他东西?

更新:

logcat说:

Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.example.bannerexample.MyActivity$PlaceholderFragment: make sure class name exists, is public, and has an empty constructor that is public

更新2:

我通过使用“com.myc.myapp.myactivity $ PlaceholderFragment”更改整行解决了这个问题

广告布局上没有任何内容,只是空白。

更新3:

我添加了一个adListener,我从onAdLoaded函数中获取了toast。但除了我的其他布局外,屏幕上没有任何内容。我用于广告的布局是空的。

mAdView.setAdListener(new AdListener() {
                @Override
                public void onAdOpened() {
                    // Save app state before going to the ad overlay.
                }

                @Override
                public void onAdFailedToLoad(int errorCode) {
                    super.onAdFailedToLoad(errorCode);
                    msg = errorCode +"";
                    Toast.makeText(c, msg, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onAdLoaded() {
                    super.onAdLoaded();
                    msg = "done";
                    Toast.makeText(c, msg, Toast.LENGTH_SHORT).show();
                }
            });

更新4:

我的课程,我想要显示添加:

public class OyunAna extends ActionBarActivity {
.
.
.
.
.
.
.
.
    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_my, container, false);
                return rootView;
            }

        }
        public static class AdFragment extends Fragment {

            private AdView mAdView;

            public AdFragment() {
            }

            @Override
            public void onActivityCreated(Bundle bundle) {
                super.onActivityCreated(bundle);

                // Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in
                // values/strings.xml.
                mAdView = (AdView) getView().findViewById(R.id.adView);

                // Create an ad request. Check logcat output for the hashed device ID to
                // get test ads on a physical device. e.g.
                // "Use AdRequest.Builder.addTestDevice("ABCDEF012345") to get test ads on this device."
                AdRequest adRequest = new AdRequest.Builder()
                        .addTestDevice("my device id goes here")
                        .build();

                // Start loading the ad in the background.
                mAdView.loadAd(adRequest);
                mAdView.setAdListener(new AdListener() {
                    @Override
                    public void onAdOpened() {
                        // Save app state before going to the ad overlay.
                    }

                    @Override
                    public void onAdFailedToLoad(int errorCode) {
                        super.onAdFailedToLoad(errorCode);
                        msg = errorCode +"";
                        Toast.makeText(c, msg, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onAdLoaded() {
                        super.onAdLoaded();
                        msg = "done";
                        Toast.makeText(c, msg, Toast.LENGTH_SHORT).show();
                    }
                });
            }
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                return inflater.inflate(R.layout.fragment_ad, container, false);
            }

            /** Called when leaving the activity */
            @Override
            public void onPause() {
                if (mAdView != null) {
                    mAdView.pause();
                }
                super.onPause();
            }

            /** Called when returning to the activity */
            @Override
            public void onResume() {
                super.onResume();
                if (mAdView != null) {
                    mAdView.resume();
                }
            }

            /** Called before the activity is destroyed */
            @Override
            public void onDestroy() {
                if (mAdView != null) {
                    mAdView.destroy();
                }
                super.onDestroy();
            }

        }

fragment_ad:     

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            ads:adSize="BANNER"
            ads:adUnitId="@string/banner_ad_unit_id" />
</RelativeLayout>

我的活动布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1"
    android:orientation="vertical"
    tools:context="com.cdev.colormatch.OyunAna"
    android:background="#FFFFFF" >

    <RelativeLayout
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.1">

        <fragment
               android:name="com.cdev.colormatch.OyunAna$PlaceholderFragment"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:layout_above="@+id/adFragment" />

           <fragment
               android:id="@+id/adFragment"
               android:name="com.cdev.colormatch.OyunAna$AdFragment"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:layout_alignParentBottom="true" />


    </RelativeLayout>
.
.
.
.
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

如果您想要测试广告,请参阅: https://developers.google.com/mobile-ads-sdk/docs/admob/android/banner#test_ads

否则,请查看您的日志,以确保正在发送广告请求以及响应是什么。