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

问题描述:

我试图在Android上显示Facebook广告。我在Android Studio项目中加入了Audience Network .jar。还将活动添加到Android Manifest。 这是的build.gradle:观众网络插页式广告/奖励广告在Unity上显示Android崩溃

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() { 
 
\t activity.runOnUiThread(new Runnable() { 
 
\t \t @Override 
 
\t \t public void run() { 
 
\t \t \t if (interstitial == null) { 
 
\t \t \t \t interstitial = new com.facebook.ads.InterstitialAd(activity, interstitialAd.getCode()); 
 
\t \t \t \t CustomFacebookInterstitialAdListener interestitialListener = new CustomFacebookInterstitialAdListener(AudienceNetworkAdProvider.this); 
 
\t \t \t \t interstitial.setAdListener(interestitialListener); 
 
\t \t \t } 
 
\t \t \t interstitial.loadAd(); 
 
\t \t } 
 
\t }); 
 
} 
 

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

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

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

的代码在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 
 
\t at com.facebook.ads.RewardedVideoAd.show(Unknown Source) 
 
\t at com.boxit.ads.facebook.AudienceNetworkAdProvider$3.run(AudienceNetworkAdProvider.java:168) 
 
\t at android.os.Handler.handleCallback(Handler.java:739) 
 
\t at android.os.Handler.dispatchMessage(Handler.java:95) 
 
\t at android.os.Looper.loop(Looper.java:158) 
 
\t at android.app.ActivityThread.main(ActivityThread.java:7229) 
 
\t at java.lang.reflect.Method.invoke(Native Method) 
 
\t at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
 
\t at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

任何想法?

我终于解决了这个问题。 我正在销毁来自我的Unity活动的onPause事件上的Audience Network广告。每次显示广告时,onPause事件都会被调用,所以这是一团糟。

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

当您下载插件,你会发现InterstitialAdSceneRewardedVideoAdScene和包含的范例代码,您应该加载InterstitialAdScene场景,因为这是你在找什么NativeAdScene场景。

下面是从InterstitialAdTest.cs C#插页式广告例子文件中AudienceNetwork \的Samples \间质性文件夹。

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"); 
    } 
} 
+0

我无法使用Facebook插件,因为我正在为Android定制广告中介。 –

+0

你的问题表明你正试图在Android上使用Unity来展示Facebook广告....为什么你不能使用已经为你写的facebook插件? – Programmer