The method must not be replaced by the superclass - java

I am trying to add Facebook Ads(sdk 6.3.0) in my Android App
The documentation on which I base myself
https://developers.facebook.com/docs/audience-network/reference/android/com/facebook/ads/interstitialad.html/?version=v6.3.0
https://developers.facebook.com/docs/audience-network/setting-up/ad-setup/android/interstitial/
I want when the person clicks on a button it shows an ad and then a new activity when the ad end
I also use a return which directly shows the activity if no ads are available
Here are the errors I am getting
The onInterstitialDisplayed method must not be replaced by the superclass
The onInterstitialDismissed method must not be replaced by the superclass
The onError method must not be replaced by the superclass
The onAdLoaded method must not be replaced by the superclass
The onAdClicked method must not be replaced by the superclass
The onAdLoaded method must not be replaced by the superclass
What I have done
private InterstitialAd mInterstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AudienceNetworkAds.initialize(this);
loadAd();
clickListener();
}
private void clickListener() {
dailyCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showInterstitialAd(1);
}
});
rewardCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showInterstitialAd(2);
}
});
}
private void loadAd(){
mInterstitialAd = new InterstitialAd(this, "750944672287722_755093405206182");
}
private void showInterstitialAd(final int i) {
if (mInterstitialAd.isAdLoaded()) {
mInterstitialAd.show();
mInterstitialAd.loadAd(new InterstitialAd.InterstitialLoadAdConfig() {
#Override
public void onInterstitialDisplayed(Ad ad) {
}
#Override
public void onInterstitialDismissed(Ad ad) {
if (i == 1)
{
dailyCheck();
}
if (i == 2)
{
startActivity(new Intent(MainActivity.this, RewardActivity.class));
}
}
#Override
public void onError(Ad ad, AdError adError) {
}
#Override
public void onAdLoaded(Ad ad) {
}
#Override
public void onAdClicked(Ad ad) {
}
#Override
public void onLoggingImpression(Ad ad) {
}
});
return;
}
if (i == 1)
{
dailyCheck();
}
if (i == 2)
{
startActivity(new Intent(MainActivity.this, RewardActivity.class));
}
}
Any help is good to take!
Thank you

Related

Is it okay to use/invoke new Handler() in a loop?

I am using new Handler().postDelayed to set a delay when reloading an ad when the previous request of the ad has failed in the requestNewInterstitial method. I am wondering if invoking/creating many new Handlers would be okay as in my loop the new Handler line of code could be run up to 5 times. My question is, if later I have to clear the handler in the Activity onDestroy method so that there are no memory leaks, should I clear it only once or should I clean all the new Handlers I have created (the number of times that the loop is run). Also, how could I implement to clear the Handler OnDestroy method?
This is my code:
#Override
protected void onResume() {
super.onResume();
requestNewInterstitial(5);
}
private void requestNewInterstitial(int maxRetry) {
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(Activityone.this, getString(R.string.interid),
adRequest, new InterstitialAdLoadCallback() {
#Override
public void onAdLoaded(#NonNull InterstitialAd interstitialAd) {
mInterstitialAd = interstitialAd;
}
#Override
public void onAdFailedToLoad(#NonNull LoadAdError loadAdError) {
if (maxRetry>0){
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
mInterstitialAd = null;
requestNewInterstitial(maxRetry-1);
}
}, 1000);
}
});
}
}
btnPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd != null) {
mInterstitialAd.show(Activityone.this);
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
#Override
public void onAdDismissedFullScreenContent() {
}
#Override
public void onAdFailedToShowFullScreenContent(#NonNull AdError adError) {
}
});
Thanks for the help.
If the delay in postDelay extends beyound the activity lifetime, the runnable will execute after the activity has been destryed and will lead app crash. You should use a common handler. To clear the handler, you can call handler.removeCallbacksAndMessages(null); in on destroy or wherever else you want to clear them.
Handler handler;
#Override
public void onCreate(#Nullable Bundle savedInstanceState, #Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
handler = new Handler();
btnPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd != null) {
mInterstitialAd.show(Activityone.this);
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
#Override
public void onAdDismissedFullScreenContent() {
}
#Override
public void onAdFailedToShowFullScreenContent(#NonNull AdError adError) {
}
});
}
}
});
}
#Override
protected void onResume() {
super.onResume();
requestNewInterstitial(5);
}
#Override
protected void onDestroy() {
handler.removeCallbacksAndMessages(null)
super.onDestroy();
}
private void requestNewInterstitial(int maxRetry) {
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(Activityone.this, getString(R.string.interid), adRequest, new InterstitialAdLoadCallback() {
#Override
public void onAdLoaded(#NonNull InterstitialAd interstitialAd) {
mInterstitialAd = interstitialAd;
}
#Override
public void onAdFailedToLoad(#NonNull LoadAdError loadAdError) {
if (maxRetry > 0) {
handler.postDelayed(new Runnable() {
#Override
public void run() {
mInterstitialAd = null;
requestNewInterstitial(maxRetry - 1);
}
}, 1000);
}
}
});
}

Interstitial advertising (admob) only works once

These days I am doing a test, the objective is to click on a button to go to activity2 and show the interstitial advertising. For now this is the result thanks to information from: link
The problem:
Upon returning to the main page (MainActivity), I pressed the button to re-enter activity2 and it is no longer displayed. Only if I close the application and open the application and press the activity button 2 the advertising is displayed.
MainActivity code
public class MainActivity extends Activity {
private InterstitialAd mInterstitialAd;
private static final String TAG = "InfoApp";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//***** Initialize the Mobile Ads SDK.*********** -//
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
//**********************************************************//
adrequest_interstitial = new AdRequest.Builder().build();
InterstitialAd.load(this,ConfigPubli.Ads_interstitial, adrequest_interstitial,
new InterstitialAdLoadCallback()
{
#Override
public void onAdLoaded(#NonNull InterstitialAd interstitialAd)
{
mInterstitialAd = interstitialAd;
Log.i(TAG, "onAdLoaded");
}
#Override
public void onAdFailedToLoad(#NonNull LoadAdError loadAdError)
{
// Handle the error
Log.i(TAG, loadAdError.getMessage());
mInterstitialAd = null;
}
});
}
public void adstot(Intent i){
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
#Override
public void onAdDismissedFullScreenContent() {
mInterstitialAd = null;
startActivity(i);
}
#Override
public void onAdFailedToShowFullScreenContent(#NonNull AdError adError) {
mInterstitialAd = null;
startActivity(i);
}
});
}
public void page2(View view){ //button
Intent i = new Intent (this, Activity2.class);
i.putExtra("valor","page2");
int randomAd = (int)(Math.random()*10);
if (mInterstitialAd != null && randomAd<=1) {
adstot(i);
mInterstitialAd.show(this);
}
else{
startActivity(i);
}
}
}
Thanks for the answers
Whenever InterstitialAd has been shown, the variable mInterstitialAd was set to null. So, how mInterstitialAd can be shown twice?
You should make a new request to get an interstitial just after mInterstitialAd = null;
Try like below:
public class MainActivity extends Activity {
private InterstitialAd mInterstitialAd;
private static final String TAG = "InfoApp";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//***** Initialize the Mobile Ads SDK.*********** -//
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
// Initialization complete
// You are now free to make an ad request
loadAnInterstitial();
}
});
//**********************************************************//
}
private void loadAnInterstitial(){
adrequest_interstitial = new AdRequest.Builder().build();
InterstitialAd.load(this,ConfigPubli.Ads_interstitial, adrequest_interstitial,
new InterstitialAdLoadCallback()
{
#Override
public void onAdLoaded(#NonNull InterstitialAd interstitialAd)
{
mInterstitialAd = interstitialAd;
Log.i(TAG, "onAdLoaded");
}
#Override
public void onAdFailedToLoad(#NonNull LoadAdError loadAdError)
{
// Handle the error
Log.i(TAG, loadAdError.getMessage());
mInterstitialAd = null;
}
});
}
public void adstot(Intent i){
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
#Override
public void onAdDismissedFullScreenContent() {
mInterstitialAd = null;
startActivity(i);
// make a new ad request here
loadAnInterstitial();
}
#Override
public void onAdFailedToShowFullScreenContent(#NonNull AdError adError) {
mInterstitialAd = null;
startActivity(i);
// make a new ad request here
loadAnInterstitial();
}
});
}
public void page2(View view){ //button
Intent i = new Intent (this, Activity2.class);
i.putExtra("valor","page2");
int randomAd = (int)(Math.random()*10);
if (mInterstitialAd != null && randomAd<=1) {
adstot(i);
mInterstitialAd.show(this);
}
else{
startActivity(i);
// make a new ad request here
loadAnInterstitial();
}
}
}
That's all. Hope it will help.
Updated code (another version):
I have added an (if), after 6 clicks of the button it will load a new ad.
public void page2(View view){ //button
Intent i = new Intent (this, Activity2.class);
i.putExtra("valor","page2");
int randomAd = (int)(Math.random()*10);
if (mInterstitialAd != null && randomAd<=1) {
adstot(i);
mInterstitialAd.show(this);
}
else{
startActivity(i);
if(countclick==6)
{
loadAnInterstitial();
}else{
countclick=countclick+1;
}
}
}
It works!, I hope that google admob does not consider that it is poorly implemented.

Custom listener with interface

I am trying to understand better how to create a custom listener with a simple example but I don't know how to start the interface so that it is not null:
public class MainActivity extends AppCompatActivity implements ListenerButton{
TextView helloToOther;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helloToOther = findViewById(R.id.helloWorldToOtherActivity);
helloToOther.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, ButtonActivity.class));
}
});
}
#Override
public void onClickButton(View view) {
Toast.makeText(this, "Estoy en la primera activity", Toast.LENGTH_SHORT).show();
}
}
This is the second activity:
public class ButtonActivity extends AppCompatActivity {
Button btnInterface;
ListenerButton listenerButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
btnInterface = findViewById(R.id.button_activity__btn__button_interface);
setUpButtonInterface();
}
private void setUpButtonInterface() {
btnInterface.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listenerButton.onClickButton(v);
}
});
}
}
And there's the interface:
public interface ListenerButton {
void onClickButton(View view);
}
Basically I get a null pointer exception on the second activity because the interface is null, but I don't fall right now as I can start it. Thank you very much.
the reason you are getting a null pointer exception is because you haven't assigned anything to variable listenerButton and therefor it is in fact null!!
you don't need a new interface for that you just need to do this:
public class ButtonActivity extends AppCompatActivity {
Button btnInterface;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
btnInterface = findViewById(R.id.button_activity__btn__button_interface);
setUpButtonInterface();
}
private void setUpButtonInterface() {
btnInterface.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do whatever you want to do when button is clicked!
}
});
}
}
if you want to define whatever you want to do when button is clicked you need a class and not an interface:
public class ButtonListener implements View.OnClickListener{
#Override
public void onClick(View v) {
//do whatever you want to do when button is clicked!
}
}
and then in your activity do this:
public class ButtonActivity extends AppCompatActivity {
Button btnInterface;
ListenerButton listenerButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
listenerButton = new ButtonListener();
btnInterface = findViewById(R.id.button_activity__btn__button_interface);
setUpButtonInterface();
}
private void setUpButtonInterface() {
btnInterface.setOnClickListener(listenerButton);
}
}
Create a new file:
MyListener.java:
public interface MyListener {
// you can define any parameter as per your requirement
public void callback(View view, String result);
}
In your activity, implement the interface:
MyActivity.java:
public class MyActivity extends Activity implements MyListener {
#override
public void onCreate(){
MyButton m = new MyButton(this);
}
// method is invoked when MyButton is clicked
#override
public void callback(View view, String result) {
// do your stuff here
}
}
In your custom class, invoke the interface when needed:
MyButton.java:
public class MyButton {
MyListener ml;
// constructor
MyButton(MyListener ml) {
//Setting the listener
this.ml = ml;
}
public void MyLogicToIntimateOthers() {
//Invoke the interface
ml.callback(this, "success");
}
}

How to stop a user from closing a reward ad with back press

I have an app that will show Admob reward videos and interstellar ads, but I noticed that a user can simply press the back button and the ad will close. is there anyway to prevent the ad from closing by just clicking the back button i tried using the onBackPressed() method with no luck
public class EarnActivity extends AppCompatActivity implements RewardedVideoAdListener{
private RewardedVideoAd mAd;
private boolean showing;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.earn);
runAd();
}
this is the method to run the ad
private void runAd(){
MobileAds.initialize(this, "App ID");
mAd = MobileAds.getRewardedVideoAdInstance(this);
mAd.setRewardedVideoAdListener(this);
loadRewardVideoAd();
mAd.show();
}
loadRewardVideoAd(){
if(!mAd.isLoaded())
{
mAd.loadAd("ad-number", new AdRequest.Builder().build());
}
}
and these are my #Override methods including the onbackpressed method
#Override
public void onRewardedVideoAdLoaded() {
}
#Override
public void onRewardedVideoAdOpened() {
}
#Override
public void onRewardedVideoStarted() {
showing = true;
}
#Override
public void onRewardedVideoAdClosed() {
loadRewardVideoAd();
showing = false;
}
#Override
public void onRewarded(RewardItem rewardItem) {
}
#Override
public void onRewardedVideoCompleted(){
}
#Override
public void onRewardedVideoAdLeftApplication() {
}
#Override
public void onRewardedVideoAdFailedToLoad(int i) {
loadRewardVideoAd();
}
#Override
public void onBackPressed() {
if(showing){
}else{
super.onBackPressed();
}
}
}
Another way, it's trying to override onKeyEvent. However, just make sure what you are doing with it.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return super.onKeyDown(keyCode, event);
}

AdapterHandler - abstract class Null Pointer Exception

I have onClickListener in my Custom Adapter for ListView
holder.name.setText(dataModelList.get(position).getRadio_name());
final ViewHolder finalHolder = holder;
holder.name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(adapterhandler !=null) {
adapterhandler.updateText(dataModelList.get(position).getRadio_name());
}
if(!mRadioManager.isPlaying()) {
mRadioManager.connect();
mRadioManager.startRadio(dataModelList.get(position).getRadio_url());
}
else
{
mRadioManager.stopRadio();
}
}
});
Then I made Abstract Class to update TextView
public abstract class InterfaceUpdate
{
public void updateText(String text) {}
}
and Listener in Fragment to update TextView:
#Override
public void onRadioStarted() {
adapter.adapterhandler = new InterfaceUpdate() {
#Override
public void updateText(String text) {
super.updateText(text);
title.setText(text);
}
};
What is my problem? I cannot make
adapterhandler.updateText(dataModelList.get(position).getRadio_name());
without checking if it's null, because it's generate NullPointerException
when I click on list element first time, TextView is not changing, second time is changing perfect ...;/ Any idea how to not make this adapterhandler null?
The problem is your adapterhandler is not being created until after the first time you try and call updateText. (It's not called until the onRadioStarted() callback is called by mRadioManager.startRadio)
Try this (although constructing your handler in onClick is not a great design)
holder.name.setText(dataModelList.get(position).getRadio_name());
final ViewHolder finalHolder = holder;
holder.name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (null == adapterhandler) {
adapterhandler = new InterfaceUpdate() {
#Override
public void updateText(String text) {
title.setText(text);
}
}
}
adapterhandler.updateText(dataModelList.get(position).getRadio_name());
if(!mRadioManager.isPlaying()) {
mRadioManager.connect();
} else {
mRadioManager.stopRadio();
}
}
});
And change your onRadioStarted:
#Override
public void onRadioStarted() {
mRadioManager.startRadio(dataModelList.get(position).getRadio_url());
};

Categories