简体   繁体   English

我如何将亚马逊广告集成到我的android应用中

[英]how can i integrating the amazon ads in my android app

I am new to the android i have downloaded amazon sdk for android. 我是Android的新手,我已经为Android下载了Amazon sdk。 I ran the sample application which have provided by amazon sdk with my app key.it was not displaying any ads it was showing unfortunately stopped message 我运行了由亚马逊sdk提供的带有我的应用程序密钥的示例应用程序。它没有显示任何广告,不幸的是它显示了已停止的消息

here is my logcat output: 这是我的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: 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: 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>

The problem is that you did not add the amazon ads library to you project. 问题是您没有将亚马逊广告库添加到您的项目中。

In the Amazon Mobile App SDK for Android, you will find a folder called lib with a jar file in it called amazon-ads-5.4.235.jar 在适用于Android的Amazon Mobile App SDK中,您将找到一个名为lib的文件夹,其中包含一个名为amazon-ads-5.4.235.jar的jar文件。

Now to add the library to your project using Android Studio: 现在使用Android Studio将库添加到您的项目中:

  1. Create a libs directory/folder under app in your project 在项目中的app下创建一个libs目录/文件夹
  2. Copy amazon-ads-5.4.235.jar to the libs folder 将amazon-ads-5.4.235.jar复制到libs文件夹
  3. Right click the jar file and click "Add As Library..." from menu 右键单击jar文件,然后从菜单中单击“添加为库...”。
  4. Rebuild your project and run app on emulator/device 重建项目并在模拟器/设备上运行应用

Now to add the library to your project using Eclipse, try this link: https://developer.amazon.com/public/apis/earn/mobile-ads/docs/quick-start 现在,要使用Eclipse将库添加到项目中,请尝试以下链接: https : //developer.amazon.com/public/apis/earn/mobile-ads/docs/quick-start

Everything should now work correctly. 现在一切都应该正常工作。 Should look like below screenshot. 应该如下图所示。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM