我如何在我的Android应用程序中集成亚马逊广告

时间:2015-01-09 07:59:54

标签: android amazon ads

我是Android的新手我已下载亚马逊sdk for android。我运行了亚马逊sdk用我的app key提供的示例应用程序。它没有显示任何广告,它显示不幸停止消息

这是我的logcat输出:

01-09 12:06:39.198: E/AndroidRuntime(25084): FATAL EXCEPTION: main
01-09 12:06:39.198: E/AndroidRuntime(25084): Process: com.amazon.sample.simplead, PID: 25084
01-09 12:06:39.198: E/AndroidRuntime(25084): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.amazon.sample.simplead/com.amazon.sample.simplead.SimpleAdActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class com.amazon.device.ads.AdLayout
01-09 12:06:39.198: E/AndroidRuntime(25084):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
01-09 12:06:39.198: E/AndroidRuntime(25084):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
01-09 12:06:39.198: E/AndroidRuntime(25084):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
01-09 12:06:39.198: E/AndroidRuntime(25084):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
01-09 12:06:39.198: E/AndroidRuntime(25084):    at android.os.Handler.dispatchMessage(Handler.java:102)
01-09 12:06:39.198: E/AndroidRuntime(25084):    at android.os.Looper.loop(Looper.java:136)
01-09 12:06:39.198: E/AndroidRuntime(25084):    at android.app.ActivityThread.main(ActivityThread.java:5086)
01-09 12:06:39.198: E/AndroidRuntime(25084):    at java.lang.reflect.Method.invokeNative(Native Method)
01-09 12:06:39.198: E/AndroidRuntime(25084):    at java.lang.reflect.Method.invoke(Method.java:515)
01-09 12:06:39.198: E/AndroidRuntime(25084):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
01-09 12:06:39.198: E/AndroidRuntime(25084):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
01-09 12:06:39.198: E/AndroidRuntime(25084):    at dalvik.system.NativeStart.main(Native Method)
01-09 12:06:39.198: E/AndroidRuntime(25084): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.amazon.device.ads.AdLayout
01-09 12:06:39.198: E/AndroidRuntime(25084):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)

SimpleAdActivity:

        public class SimpleAdActivity extends Activity {
            private AdLayout adView; // The ad view used to load and display the ad.
            private static final String APP_KEY = "key"; // Sample Application Key. Replace this value                         with your Application Key.
            private static final String LOG_TAG = "SimpleAdSample"; // Tag used to prefix all log messages.

            /**
             * When the activity starts, load an ad and set up the button's click event to load another ad when it's clicked.
             */
            @Override
            public void onCreate(final Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

                // For debugging purposes enable logging, but disable for production builds.
                AdRegistration.enableLogging(true);
                // For debugging purposes flag all ad requests as tests, but set to false for production builds.
                AdRegistration.enableTesting(true);

                this.adView = (AdLayout) findViewById(R.id.ad_view);
                this.adView.setListener(new SampleAdListener());

                try {
                    AdRegistration.setAppKey(APP_KEY);
                } catch (final IllegalArgumentException e) {
                    Log.e(LOG_TAG, "IllegalArgumentException thrown: " + e.toString());
                    return;
                }

                // Assign an onClick handler to the button that will load our ad.
                final Button button = (Button) findViewById(R.id.load_ad_button);
                button.setOnClickListener(new View.OnClickListener() {
                    public void onClick(final View v) {
                        loadAd();
                    }
                });

                // Calling load ad in the Activity's onCreate method allows a new ad to be loaded 
                // when the application starts and also when the device is rotated.
                loadAd();
            }

            /**
             * Load a new ad.
             */
            public void loadAd() {
                // Load an ad with default ad targeting.
                this.adView.loadAd();

                // Note: You can choose to provide additional targeting information to modify how your ads
                // are targeted to your users. This is done via an AdTargetingOptions parameter that's passed
                // to the loadAd call. See an example below:
                //
                //    final AdTargetingOptions adOptions = new AdTargetingOptions();
                //    adOptions.enableGeoLocation(true);
                //    this.adView.loadAd(adOptions);
            }

            /**
             * This class is for an event listener that tracks ad lifecycle events.
             * It extends DefaultAdListener, so you can override only the methods that you need.
             */
            class SampleAdListener extends DefaultAdListener {
                /**
                 * This event is called once an ad loads successfully.
                 */
                @Override
                public void onAdLoaded(final Ad ad, final AdProperties adProperties) {
                    Log.i(LOG_TAG, adProperties.getAdType().toString() + " ad loaded successfully.");
                }
                /**
                 * This event is called if an ad fails to load.
                 */
                @Override
                public void onAdFailedToLoad(final Ad ad, final AdError error) {
                    Log.w(LOG_TAG, "Ad failed to load. Code: " + error.getCode() + ", Message: " + error.getMessage());
                }
                 /**
                 * This event is called after a rich media ad expands.
                 */
                @Override
                public void onAdExpanded(final Ad ad) {
                    Log.i(LOG_TAG, "Ad expanded.");
                    // You may want to pause your activity here.
                }

                /**
                 * This event is called after a rich media ad has collapsed from an expanded state.
                 */
                @Override
                public void onAdCollapsed(final Ad ad) {
                    Log.i(LOG_TAG, "Ad collapsed.");
                    // Resume your activity here, if it was paused in onAdExpanded.
                }
            }
        }

main.xml中:

    <?xml version="1.0" encoding="utf-8"?>

     <LinearLayout 
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:Amazon="http://schemas.android.com/apk/lib/com.amazon.device.ads"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical"
         android:gravity="top|center_horizontal">

         <com.amazon.device.ads.AdLayout

            android:id="@+id/ad_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:orientation="vertical"
             android:gravity="bottom|center">

             <Button
                 android:id="@+id/load_ad_button"
                 android:layout_width="150dp"
                 android:layout_height="50dp"
                 android:text="@string/button_text"
                 android:textSize="8.5pt">
             </Button>

             <TextView
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:layout_marginTop="30dp"
                 android:layout_marginBottom="30dp"
                 android:gravity="center"
                 android:textSize="6.5pt"
                 android:padding="5dp"
                 android:text="@string/description">
             </TextView>

         </LinearLayout>    

     </LinearLayout>

1 个答案:

答案 0 :(得分:1)

问题是您没有将亚马逊广告库添加到项目中。

在适用于Android的亚马逊移动应用SDK中,您会找到一个名为lib的文件夹,其中包含一个名为amazon-ads-5.4.235.jar的jar文件

现在使用Android Studio将库添加到项目中:

  1. 在项目中的app下创建一个libs目录/文件夹
  2. 将amazon-ads-5.4.235.jar复制到libs文件夹
  3. 右键单击jar文件,然后从菜单
  4. 中单击“Add As Library ...”
  5. 重建项目并在模拟器/设备上运行应用程序
  6. 现在使用Eclipse将库添加到项目中,请尝试以下链接: https://developer.amazon.com/public/apis/earn/mobile-ads/docs/quick-start

    现在一切都应该正常工作了。应该如下图所示。

    enter image description here