1

I am trying to call a runTheAd() method which is defined in MainActivity.kt from another Activity class RewardedAd.kt

I have simply followed this guide Google Admob Doc

Function in MainActivity.kt

fun runTheAd(){
        if (mRewardedVideoAd.isLoaded) {
            mRewardedVideoAd.show()
        }else{
            loadRewardedVideoAd()
        }
    }

Calling in RewardAd.kt

getPoints.setOnClickListener{
            MainActivity().runTheAd()
        }

Erron Getting

   E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.takshaksh.layoutout, PID: 28485
        kotlin.UninitializedPropertyAccessException: lateinit property mRewardedVideoAd has not been initialized
            at com.takshaksh.layoutout.MainActivity.runTheAd(MainActivity.kt:56)
            at com.takshaksh.layoutout.RewardedAd$onCreate$1.onClick(RewardedAd.kt:20)
Takshak Rajput
  • 119
  • 1
  • 14

3 Answers3

0

You need to create an instance first. After that call show method.

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713")

    // Use an activity context to get the rewarded video instance.
    mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this)
    mRewardedVideoAd.rewardedVideoAdListener = this
}

Also as I remember you need to setup adMob side to activate your ad's ID and some manipulation with manifest. So somewhere on this steps the connection is not working what's why instance not created and the result is error.

Bo Z
  • 2,359
  • 1
  • 13
  • 31
  • You are right that an instance is required but that is already done in MainActivity.kt file. What am I asking is that on calling a defined function in MainActivity.kt from Another activity file that is RewardAd.kt in this case, I am getting the error on button press from RewardAd.kt activity – Takshak Rajput Jun 14 '19 at 20:16
  • @TakshakRajput why did you do so? can you explain logic of separate activity? – Bo Z Jun 14 '19 at 20:17
  • I just want to define and initialize the things once so that I can simply use a function to display the ad only any activity. – Takshak Rajput Jun 14 '19 at 20:31
  • @TakshakRajput for that purpose I did separate class which extended from application where I initialize that kind of instance. Ad or analytics for example and can call it from anywhere you want – Bo Z Jun 14 '19 at 20:42
0

since you are calling the method from another activity, second activity is not able to initialize mRewardedAd from MainActivity. So you can put initialization in the runTheAd method, however it's not the best practice.

Payam Asefi
  • 2,677
  • 2
  • 15
  • 26
0

you just cannot create and store activity instance like that. instead, you just start your second activity with

**startActivityForResult**(intent,101);

then in your first activity just override the

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
    // Check which request we're responding to
    if (requestCode == 101) {
        //here do start your ad when some condition is satisfied
    }
}

in your second activity when you think or just some condition is met just call this

Intent intent = new Intent();
intent.putExtra("key", "I am done");
setResult(RESULT_OK, intent);

this will trigger the onActivityResult in first activity given the activity is in back stack. where you can write your logic to start ad

The second option is to create some local broadcast and receive them very similar to the above using the intents again.

The third option you can have is Event bus which is again very simple in implementation as well

here you can read more.

Really not getting setResult and onActivityResult

How to use LocalBroadcastManager?

https://github.com/greenrobot/EventBus

vikas kumar
  • 10,447
  • 2
  • 46
  • 52
  • I am not sure how this solves the problem and this is in Kotlin. – Takshak Rajput Jun 14 '19 at 20:33
  • In java I can simply define a function in MainActivity like this : public static void runTheAd(){} and then call it from any activity like this MainActivity.runTheAd() and it works fine – Takshak Rajput Jun 14 '19 at 20:37
  • the way you are doing it may not be recommended I guess, and I have changed the statements to kotlin. Anyway if you are okay with your solution then there is nothing to change. good luck – vikas kumar Jun 15 '19 at 06:06