1

大家好,我在我的 mainactivity 中集成了 admob 横幅广告并且工作得很好,但是当我尝试添加 admob 插页式广告时,它会导致应用程序在您打开它时崩溃,在构建 apk 时没有显示错误什么 ami 做错了什么

我只是Java编码的初学者,所以我对环境不太熟悉帮助朋友

 package this.atest.packagename.myapplication;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.sax.StartElementListener;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;

public class MainActivity extends AppCompatActivity {

    InterstitialAd mInterstitialAd;
    private InterstitialAd interstitial;

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




        AdRequest adRequest1 = new AdRequest.Builder().build();

        // Prepare the Interstitial Ad
        interstitial = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
        interstitial.setAdUnitId(getString(R.string.interstitial));

        interstitial.loadAd(adRequest1);
// Prepare an Interstitial Ad Listener
        interstitial.setAdListener(new AdListener() {
            public void onAdLoaded() {
// Call displayInterstitial() function
                displayInterstitial();
            }
        });
        Button btn1 =(Button)findViewById(R.id.btn1);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intent);

            }
        });



        AdView adView = (AdView) findViewById(R.id.adViewBanner);
        AdRequest adRequest = new AdRequest.Builder()
                .setRequestAgent("android_studio:ad_template").build();
        adView.loadAd(adRequest);



    }


    public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
        if (interstitial.isLoaded()) {
            interstitial.show();
        }
    }

}
4

3 回答 3

0

你有没有在你的清单中添加这个?

<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />
于 2017-08-11T03:12:35.470 回答
0

你必须试试这个

interstitial.setAdUnitId(getResources().getString(R.string.intertitial));

有时 getstring 不获取,所以这很有帮助。

于 2017-08-11T06:06:26.550 回答
0

您只需在您的 xml 文件中检查它应该是

xmlns:ads="http://schemas.android.com/apk/res-auto"

ads:loadAdOnCreate="true"
于 2017-08-11T04:24:41.980 回答