0

I'm trying to show AdColony's rewarded video to my app when the user opens the ResultActivity and I request ad on MainActivity and load it to ad on MainActivity as below by calling the related functions from onCreate function.

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();
}

Then I call the showRewardedVideo() function from ResultActivity's onCreate function as below:

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

      MainActivity.showRewardedVideo();
   }
}

When I open the Result Activity I got the error below and the app stop executing and quits:

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)

All of the helps will be appreciated. Cheers.

  • Either onRequestFilled is not being called or it is being called after onCreate - add some logging to get an idea of the timing and then put something in place to force things to line up as you intend – Paul MacGuiheen Jan 26 '18 at 13:48
  • Yeah I tried that, clearly it doesn't enter into onRequestFilled function before onCreate function. Now trying to find a way to force it to enter – Sarlken Konig Jan 26 '18 at 14:19
  • how to know if ad is loaded or not to show? – Anand Savjani Nov 19 '18 at 17:09

0 Answers0