0

当用户打开 ResultActivity 并且我在 MainActivity 上请求广告并通过调用 onCreate 函数中的相关函数将其加载到 MainActivity 上的广告时,我正在尝试向我的应用程序显示 AdColony 的奖励视频。

public class MainActivity extends AppCompatActivity {

 private static final String APP_ID = "app342fa159f9994596a1";
 private static final String RV_ZONE_ID = "vzeeb9e8d85dab4e2894";
 private static AdColonyInterstitial rvAd;
 private static AdColonyInterstitialListener rvListener;
 private static AdColonyAdOptions ad_options;
 private String uniqueID;

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

      this.initAdColony();
      this.loadRewardedVideo();
}

public void initAdColony(){
    uniqueID = UUID.randomUUID().toString();

    final AdColonyAppOptions app_options = new AdColonyAppOptions().setUserID(uniqueID);

    AdColony.configure(this, app_options, APP_ID, RV_ZONE_ID);

    ad_options = new AdColonyAdOptions()
            .enableConfirmationDialog( true )
            .enableResultsDialog( true );

    AdColony.setRewardListener( new AdColonyRewardListener()
    {
        @Override
        public void onReward( AdColonyReward reward )
        {
            reward.getRewardAmount();
        }
    } );

    rvListener = new AdColonyInterstitialListener()
    {
        @Override
        public void onRequestFilled( AdColonyInterstitial ad )
        {
            rvAd = ad;
        }

        /** Ads are not available */
        @Override
        public void onRequestNotFilled( AdColonyZone zone )
        {
        }

        /** Request a new ad if ad is expiring */
        @Override
        public void onExpiring( AdColonyInterstitial ad )
        {
            AdColony.requestInterstitial( RV_ZONE_ID, rvListener, ad_options);
        }
    };
    AdColony.requestInterstitial( RV_ZONE_ID, rvListener, ad_options );
}

public static void loadRewardedVideo(){
    AdColony.requestInterstitial( RV_ZONE_ID, rvListener, ad_options );
}

public static void showRewardedVideo(){
    rvAd.show();
}

然后我从 ResultActivity 的 onCreate 函数中调用 showRewardedVideo() 函数,如下所示:

public class ResultActivity extends AppCompatActivity{
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_result);

      MainActivity.showRewardedVideo();
   }
}

当我打开结果活动时,出现以下错误,应用程序停止执行并退出:

FATAL EXCEPTION: main                                                                       
  java.lang.RuntimeException: Unable to start activity 
  ComponentInfo{.ResultActivity}: java.lang.NullPointerException: Attempt to 
  invoke virtual method 'boolean com.adcolony.sdk.AdColonyInterstitial.show()' 
  on a null object reference.                                                                          
  Android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
  android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
  android.app.ActivityThread.-wrap14(ActivityThread.java)
  android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
  android.os.Handler.dispatchMessage(Handler.java:102)
  android.os.Looper.loop(Looper.java:154)
  android.app.ActivityThread.main(ActivityThread.java:6776)
  java.lang.reflect.Method.invoke(Native Method) 
  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 
  'boolean com.adcolony.sdk.AdColonyInterstitial.show()' on a null object 
  reference
  at .MainActivity.showRewardedVideo(MainActivity.java:240)
  at .ResultActivity.onCreate(ResultActivity.java:31)

所有的帮助将不胜感激。干杯。

4

0 回答 0