admob 的问题

Problem with admob(admob 的问题)
本文介绍了admob 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK, I had asked a similar question before and had got an answer but that was too general a question.

Now I've an app in which there are plenty of activities. Each activity has the same admob (AdView) layout being included in its layout file. Now the problem is when I go from one activity to the other after the first screen has finished loading the ad, the second activity still waits for another ad fetch cycle to happen [ie., it again sends an ad request and displays a new ad]. All I want to do is for my app to show the same instance of the ad in every activity. [Same instance meaning: I have a time interval based on which the ads must refresh, so a new ad request must be sent only when the time limit expires, not when user navigates from one activity to another.]

Is there anyway I can do this. I have tried the "Singleton" approach mentioned in the earlier solution but there are lot of complications cos everytime I do that, it says that the specified child already has a parent and a call to removeView on the parent is necessary.

Am I doing anything wrong (OR/AND) can anybody help me with some other solution??

My Singleton class is here:

public class CommonAdFooter {
static final CommonAdFooter commonAdFooter = new CommonAdFooter();
static AdView admobView;
LayoutInflater LInflater;

private CommonAdFooter() {
    LInflater = (LayoutInflater) Constants.context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    admobView = (AdView) LInflater.inflate(R.layout.ad_layout, null);
    LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    admobView.setLayoutParams(lp);
}

public static AdView getAdLayout() {
    return admobView;
}
}

and this is my layout file for the ads

<?xml version="1.0" encoding="utf-8"?>
<com.admob.android.ads.AdView
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="..."
android:id="@+id/ad" android:layout_alignParentBottom="true"
android:background="#C9E3F6" android:layout_width="fill_parent"
android:layout_height="wrap_content" myapp:backgroundColor="#006699"
myapp:primaryTextColor="#C9E3F6" myapp:secondaryTextColor="#C9E3F6" />

Edit: Admob API link added.

解决方案

I'm not sure on the exact syntax, could you link the AdMob api?

But your getting an error because when you return the ad layout it is already attached to the previous activity. So you would need something like this:

public static AdView getAdLayout() {

admobView.removeParent(); // or similar see API

return admobView;

}

EDIT

Ah here we go: AdView JavaDoc so it herits from view and RelativeLayout great.

Try this:

public static AdView getAdLayout() {
     if(admobView.getParent() != null){
        admobView.detachAllViewsFromParent();
     }
    return admobView;
}

or

public static AdView getAdLayout() {
     if(admobView.getParent() != null){
        admobView.getParent().removeView(admobView);
     }
    return admobView;
}

The answer is in the JavaDoc just a bit of trial and error

这篇关于admob 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)