广告尺寸和广告单元ID必须在调用loadAd之前设置 - 同一个类中的两个广告

时间:2016-06-26 09:00:30

标签: java android admob android-studio-2.0

我在android studio中创建了一个基本的应用程序,看看我是否可以展示admobs广告。在consructors中有两个广告,其中一个是横幅,第二个是插页式广告。如果我只使用一个广告代码来执行应用程序,无论是横幅广告还是插页式广告,都没有问题,但如果我执行下面的广告,则会抛出许多错误,其中一个是

`Caused by: java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.` 

java代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        reklamGoster = (Button)findViewById(R.id.btn_1);
        reklamYukle = (Button)findViewById(R.id.btn_2);

        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-842323422342342342342343245163920/1234233248096");
        AdRequest adRequestInter = new AdRequest.Builder().build();
        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                mInterstitialAd.show();
                Toast.makeText(getApplicationContext(), "Reklam Yüklendi.", Toast.LENGTH_LONG).show();

                reklamGoster.setText("Reklam GösterRR");
                reklamGoster.setEnabled(true);
            }

            @Override
            public void onAdFailedToLoad(int errorCode) { //Geçiş Reklam Yüklenemediğinde  Çalışır
                Toast.makeText(getApplicationContext(), "Reklam Yüklenirken Hata Oluştu.", Toast.LENGTH_LONG).show();

                reklamGoster.setText("Reklam Yüklenemedi"+errorCode);
                reklamGoster.setEnabled(false);
                mInterstitialAd.show();
            }

        });
        mInterstitialAd.loadAd(adRequestInter);

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

    }

完成错误:

06-26 11:56:23.155 5821-5821/com.example.c0b4n.myapplication E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.c0b4n.myapplication.MainActivity.access$super
06-26 11:56:23.165 5821-5821/com.example.c0b4n.myapplication E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.c0b4n.myapplication.MainActivity.access$super
06-26 11:56:23.165 5821-5821/com.example.c0b4n.myapplication E/dalvikvm: Could not find class 'android.media.session.MediaController', referenced from method com.example.c0b4n.myapplication.MainActivity.access$super
06-26 11:56:23.165 5821-5821/com.example.c0b4n.myapplication E/dalvikvm: Could not find class 'android.widget.Toolbar', referenced from method com.example.c0b4n.myapplication.MainActivity.access$super
06-26 11:56:23.185 5821-5821/com.example.c0b4n.myapplication E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method com.example.c0b4n.myapplication.MainActivity.access$super
06-26 11:56:23.195 5821-5821/com.example.c0b4n.myapplication E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.c0b4n.myapplication.MainActivity.access$super
06-26 11:56:23.195 5821-5821/com.example.c0b4n.myapplication E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.c0b4n.myapplication.MainActivity.access$super
06-26 11:56:23.215 5821-5821/com.example.c0b4n.myapplication E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.c0b4n.myapplication.MainActivity.access$super
06-26 11:56:23.235 5821-5821/com.example.c0b4n.myapplication E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method com.example.c0b4n.myapplication.MainActivity.access$super
06-26 11:56:23.235 5821-5821/com.example.c0b4n.myapplication E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method com.example.c0b4n.myapplication.MainActivity.access$super
06-26 11:56:23.245 5821-5821/com.example.c0b4n.myapplication E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.c0b4n.myapplication.MainActivity.access$super
06-26 11:56:24.745 5821-5821/com.example.c0b4n.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.example.c0b4n.myapplication, PID: 5821
                                                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.c0b4n.myapplication/com.example.c0b4n.myapplication.MainActivity}: java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                                                   at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:136)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                   at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                                                   at dalvik.system.NativeStart.main(Native Method)
                                                                                Caused by: java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.
                                                                                   at com.google.android.gms.ads.internal.client.zzz.zzda(Unknown Source)
                                                                                   at com.google.android.gms.ads.internal.client.zzz.zza(Unknown Source)
                                                                                   at com.google.android.gms.ads.BaseAdView.loadAd(Unknown Source)
                                                                                   at com.google.android.gms.ads.AdView.loadAd(Unknown Source)
                                                                                   at com.example.c0b4n.myapplication.MainActivity.onCreate(MainActivity.java:61)
                                                                                   at android.app.Activity.performCreate(Activity.java:5231)
                                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                                                   at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:136) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                                                   at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                   at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                                                   at dalvik.system.NativeStart.main(Native Method) 

在结论中,请帮助:)

0 个答案:

没有答案