观众网络插页/奖励广告显示Android上的Android崩溃

时间:2017-08-08 19:21:15

标签: android unity3d facebook-audience-network

我试图在Android上展示Facebook广告。我在我的Android Studio项目中包含了Audience Network .jar。还将活动添加到Android Manifest。 这是build.gradle:



android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    testOptions {
        unitTests.returnDefaultValues = true
    }
    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            debuggable false
        }
        debug {
            debuggable true
            jniDebuggable true
            minifyEnabled false
            shrinkResources false
        }
    }
}

dependencies {
    // Required -- JUnit 4 framework
    testCompile 'junit:junit:4.12'
    // Optional -- Mockito framework
    testCompile 'org.mockito:mockito-core:1.10.19'
    compile 'com.google.android.gms:play-services:10.2.0'
    compile files('libs/AudenceNetwork-4.24.0.jar')
    compile 'com.android.support:appcompat-v7:25.0.0'
    compile 'com.android.support:recyclerview-v7:25.0.0'
}




这是关于我如何加载和显示插页式广告并获得奖励的摘录:



com.facebook.ads.InterstitialAd interstitial;
com.facebook.ads.RewardedVideoAd rewarded

public void loadInterstitial() {
	activity.runOnUiThread(new Runnable() {
		@Override
		public void run() {
			if (interstitial == null) {
				interstitial = new com.facebook.ads.InterstitialAd(activity, interstitialAd.getCode());
				CustomFacebookInterstitialAdListener interestitialListener = new CustomFacebookInterstitialAdListener(AudienceNetworkAdProvider.this);
				interstitial.setAdListener(interestitialListener);
			}
			interstitial.loadAd();
		}
	});
}

public void showInterstitial() {
	if (isInterstitialAvailable()) {
		activity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				interstitial.show();
			}
		});
	}
}

public void loadRewardedAd() {
	activity.runOnUiThread(new Runnable() {
		@Override
		public void run() {
			if (rewarded == null) {
				rewarded = new com.facebook.ads.RewardedVideoAd(activity, rewardedAd.getCode());
				CustomFacebookRewardedAdListener rewardedListener = new CustomFacebookRewardedAdListener(AudienceNetworkAdProvider.this);
				rewarded.setAdListener(rewardedListener);
			}
			rewarded.loadAd();
		}
	});
}

public void showRewardedAd() {
	if (isRewardedAdAvailable()) {
		activity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				rewarded.show();
			}
		});
	}
}




该代码适用于Android原生测试应用程序。

我正在改编Unity,因此我可以在游戏中展示广告。但是,每当我想要显示广告时,它会随机崩溃应用程序。大多数时候,它让我看到一个插页式广告或奖励,但在那之后崩溃。

这就是我的插件/ Android文件夹的样子:



AndroidManifest.xml
appcompat-v7-24.0.0
myaudiencenetworkadapter.aar
play-services-10.0.1.aar
play-services-auth-10.0.1
play-services-auth-base-10.0.1
play-services-basement-10.0.1
play-services-drive-10.0.1
play-services-tasks-10.0.1
recyclerview-v7-24.0.0
support-v4-24.0.0




调用 com.facebook.ads.RewardedVideoAd.show()方法时崩溃。 这是崩溃的日志:



08-07 16:51:30.861 16820-16820/? E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.somepackage.test, PID: 16820
   java.lang.Error: FATAL EXCEPTION [main]
   Unity version     : 5.5.3f1
   Device model      : samsung SM-G925I
   Device fingerprint: samsung/zeroltedv/zerolte:6.0.1/MMB29K/G925IDVS3EQF1:user/release-keys
   
   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.facebook.ads.internal.DisplayAdController.c()' on a null object reference
	   at com.facebook.ads.RewardedVideoAd.show(Unknown Source)
	   at com.boxit.ads.facebook.AudienceNetworkAdProvider$3.run(AudienceNetworkAdProvider.java:168)
	   at android.os.Handler.handleCallback(Handler.java:739)
	   at android.os.Handler.dispatchMessage(Handler.java:95)
	   at android.os.Looper.loop(Looper.java:158)
	   at android.app.ActivityThread.main(ActivityThread.java:7229)
	   at java.lang.reflect.Method.invoke(Native Method)
	   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
	   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)




有什么想法吗?

2 个答案:

答案 0 :(得分:1)

当有一个官方的Unity Facebook插件时,你不应该在Android Studio中弄乱Java和Facebook jar。有关如何设置的说明,请参阅此link;有关获取实际SDK的信息,请参见here

下载插件时,您会发现 InterstitialAdScene RewardedVideoAdScene NativeAdScene 场景,其中包含您应加载 InterstitialAdScene的示例代码场景,因为那是你想要的。

以下是 AudienceNetwork \ Samples \ Interstitial 文件夹中 InterstitialAdTest.cs 文件的C#插页式广告示例。

public class InterstitialAdTest : MonoBehaviour
{

    private InterstitialAd interstitialAd;
    private bool isLoaded;

    // UI elements in scene
    public Text statusLabel;

    // Load button
    public void LoadInterstitial ()
    {
        this.statusLabel.text = "Loading interstitial ad...";

        // Create the interstitial unit with a placement ID (generate your own on the Facebook app settings).
        // Use different ID for each ad placement in your app.
        InterstitialAd interstitialAd = new InterstitialAd ("YOUR_PLACEMENT_ID");
        this.interstitialAd = interstitialAd;
        this.interstitialAd.Register (this.gameObject);

        // Set delegates to get notified on changes or when the user interacts with the ad.
        this.interstitialAd.InterstitialAdDidLoad = (delegate() {
            Debug.Log ("Interstitial ad loaded.");
            this.isLoaded = true;
            this.statusLabel.text = "Ad loaded. Click show to present!";
        });
        interstitialAd.InterstitialAdDidFailWithError = (delegate(string error) {
            Debug.Log ("Interstitial ad failed to load with error: " + error);
            this.statusLabel.text = "Interstitial ad failed to load. Check console for details.";
        });
        interstitialAd.InterstitialAdWillLogImpression = (delegate() {
            Debug.Log ("Interstitial ad logged impression.");
        });
        interstitialAd.InterstitialAdDidClick = (delegate() {
            Debug.Log ("Interstitial ad clicked.");
        });

        // Initiate the request to load the ad.
        this.interstitialAd.LoadAd ();
    }

    // Show button
    public void ShowInterstitial ()
    {
        if (this.isLoaded) {
            this.interstitialAd.Show ();
            this.isLoaded = false;
            this.statusLabel.text = "";
        } else {
            this.statusLabel.text = "Ad not loaded. Click load to request an ad.";
        }
    }

    void OnDestroy ()
    {
        // Dispose of interstitial ad when the scene is destroyed
        if (this.interstitialAd != null) {
            this.interstitialAd.Dispose ();
        }
        Debug.Log ("InterstitialAdTest was destroyed!");
    }

    // Next button
    public void NextScene ()
    {
        SceneManager.LoadScene ("AdViewScene");
    }
}

答案 1 :(得分:0)

我终于解决了这个问题。 我正在从Unity活动中销毁onPause事件中的Audience Network广告。每次显示广告时,都会调用onPause事件,因此它很乱。