我在使用AdMob在我的应用中展示广告时遇到了一些问题.问题如主题中所述.
这是我的开始活动课程:
package com.example.admobinterstitial;import androID.os.Bundle;import com.Google.androID.gms.ads.AdListener;import com.Google.androID.gms.ads.AdRequest;import com.Google.androID.gms.ads.InterstitialAd;import androID.support.v4.app.FragmentActivity;import androID.vIEw.Window;import androID.vIEw.WindowManager;public class MainActivity extends FragmentActivity{ public static InterstitialAd mInterstitialAd; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { // Super super.onCreate(savedInstanceState); // Turn off the window's Title bar requestwindowFeature(Window.FEATURE_NO_Title); // Fullscreen mode getwindow().setFlags(WindowManager.LayoutParams.FLAG_FulLSCREEN, WindowManager.LayoutParams.FLAG_FulLSCREEN); setContentVIEw(R.layout.activity_main); mInterstitialAd = new InterstitialAd(this); mInterstitialAd.setAdUnitID(getResources().getString(R.string.loading_add_ID)); mInterstitialAd.setAdListener(new AdListener() { @OverrIDe public voID onAdClosed() { requestNewInterstitial(); } }); requestNewInterstitial(); if (findVIEwByID(R.ID.fragment_container) != null) { if (savedInstanceState != null) { return; } // Create a new Fragment to be placed in the activity layout Fragments.MenuFragment firstFragment = new Fragments.MenuFragment(); // In case this activity was started with special instructions from an // Intent, pass the Intent's extras to the fragment as arguments firstFragment.setArguments(getIntent().getExtras()); // Add the fragment to the 'fragment_container' FrameLayout getSupportFragmentManager().beginTransaction() .add(R.ID.fragment_container, firstFragment, firstFragment.getClass().toString()).commit(); } } private voID requestNewInterstitial() { AdRequest adRequest = new AdRequest.Builder() .addTestDevice("my test device ID blabla") .build(); mInterstitialAd.loadAd(adRequest); }}
主要活动布局:
relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <FrameLayout androID:ID="@+ID/fragment_container" androID:orIEntation="horizontal" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="@color/bg_color" > </FrameLayout></relativeLayout>
这是我的应用程序中的2个片段:
package com.example.admobinterstitial;import androID.app.Activity;import androID.content.Context;import androID.os.Bundle;import androID.support.v4.app.Fragment;import androID.support.v4.app.FragmentActivity;import androID.support.v4.app.FragmentManager;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.button;public class Fragments{ public static class MenuFragment extends Fragment { Context mContext; @OverrIDe public voID onAttach(Activity activity) { super.onAttach(activity); mContext = activity; } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment final VIEw rootVIEw = inflater.inflate(R.layout.menu_fragment, container, false); button mybutton = (button) rootVIEw.findVIEwByID(R.ID.mybutton); mybutton.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { NextFragment newFragment = new NextFragment(); FragmentManager man = ((FragmentActivity)mContext).getSupportFragmentManager(); androID.support.v4.app.FragmentTransaction transaction = man.beginTransaction(); transaction.replace(R.ID.fragment_container, newFragment, newFragment.getClass().toString()); transaction.addToBackStack(null); transaction.commit(); } }); return rootVIEw; } }; public static class NextFragment extends Fragment { @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment final VIEw rootVIEw = inflater.inflate(R.layout.next_fragment, container, false); //Todo After showing the ad here, vIEws in my R.layout.next_fragment won't show up after closing the ad. //With those 2 lines commented, the vIEws show up correctly... if (MainActivity.mInterstitialAd.isLoaded()) MainActivity.mInterstitialAd.show(); return rootVIEw; } };}
在MainActivity中,我只是将我的R.ID.fragment_container设置为我的MenuFragment.在MenuFragment中,我有一个启动NextFragment的按钮(用这个片段替换R.ID.fragment_container).问题出在OnCreateVIEw的NextFragment类中…
我已经花了很多时间来解决这个问题,现在我非常绝望,我甚至准备了一个示例小日食项目来展示这个问题:
https://drive.google.com/file/d/0ByRNGNhz-adOQ091OU15c3ZUckE/view?usp=sharing
您必须在strings.xml中更改广告单元ID,并在requestNewInterstitial方法中在MainActivity.java中设置您的测试设备ID以显示广告…(并且可能修复一些链接错误? – 不确定Google play服务是否会为你们正确联系)
而且我会给那些解决问题的人提供奖励……
更新1:在我的一个设备上 – 三星galaxy s4 mini – 无论我使用测试广告还是真实广告,都会出现问题.但似乎在我的另一台设备LG-D320n上,问题似乎只发生在使用测试广告时……我现在完全糊涂了……
更新2:添加应用程序的完整logcat(在应用程序中没有任何崩溃,但抛出了大量消息.关闭广告后,只有没有视图,我使用“后退按钮”手动退出应用程序)
11-05 13:26:13.018: W/GooglePlayServicesUtil(15088): Google Play services out of date. Requires 8115000 but found 508403411-05 13:26:13.018: W/Ads(15088): Using InterstitialAdManager from the clIEnt jar.11-05 13:26:13.018: I/Ads(15088): Starting ad request.11-05 13:26:13.068: W/GooglePlayServicesUtil(15088): Google Play services out of date. Requires 8115000 but found 508403411-05 13:26:13.068: E/GooglePlayServicesUtil(15088): GooglePlayServices not available due to error 211-05 13:26:13.108: I/ActivityManager(15088): Timeline: Activity_IDle ID: androID.os.BinderProxy@4205cbf0 time:10732905411-05 13:26:13.188: I/Ads(15088): Not on service, return11-05 13:26:13.488: I/chromium(15088): [INFO:CONSolE(0)] "document was loaded from Application Cache with manifest https://Googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.appcache", source: https://Googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.HTML (0)11-05 13:26:13.498: I/chromium(15088): [INFO:CONSolE(0)] "Application Cache Checking event", source: https://Googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.HTML (0)11-05 13:26:13.498: I/chromium(15088): [INFO:CONSolE(0)] "Application Cache Checking event", source: https://Googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.HTML (0)11-05 13:26:13.498: I/chromium(15088): [INFO:CONSolE(0)] "Application Cache NoUpdate event", source: https://Googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.HTML (0)11-05 13:26:13.498: I/chromium(15088): [INFO:CONSolE(0)] "Application Cache NoUpdate event", source: https://Googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.HTML (0)11-05 13:26:13.968: D/dalvikvm(15088): GC_FOR_ALLOC freed 1811K, 26% free 9199K/12400K, paused 52ms, total 52ms11-05 13:26:14.038: W/AwContents(15088): nativeOnDraw Failed; clearing to background color.11-05 13:26:14.148: I/chromium(15088): [INFO:async_pixel_transfer_manager_androID.cc(56)] Async pixel transfers not supported11-05 13:26:14.218: I/chromium(15088): [INFO:async_pixel_transfer_manager_androID.cc(56)] Async pixel transfers not supported11-05 13:26:14.458: I/Ads(15088): Ad finished loading.11-05 13:26:15.778: I/VIEwRootImpl(15088): VIEwRoot's touch Event : touch Down11-05 13:26:15.898: I/VIEwRootImpl(15088): VIEwRoot's touch Event : touch UP11-05 13:26:15.958: D/dalvikvm(15088): GC_FOR_ALLOC freed 2271K, 41% free 7316K/12400K, paused 31ms, total 32ms11-05 13:26:16.308: I/ActivityManager(15088): Timeline: Activity_launch_request ID:com.example.admobinterstitial time:10733225111-05 13:26:16.318: I/Ads(15088): Ad opening.11-05 13:26:16.378: D/dalvikvm(15088): GC_FOR_ALLOC freed 1553K, 40% free 7450K/12400K, paused 23ms, total 23ms11-05 13:26:16.418: I/chromium(15088): [INFO:async_pixel_transfer_manager_androID.cc(56)] Async pixel transfers not supported11-05 13:26:16.488: I/chromium(15088): [INFO:async_pixel_transfer_manager_androID.cc(56)] Async pixel transfers not supported11-05 13:26:16.738: I/ActivityManager(15088): Timeline: Activity_IDle ID: androID.os.BinderProxy@41f92ef0 time:10733268411-05 13:26:18.418: I/VIEwRootImpl(15088): VIEwRoot's touch Event : touch Down11-05 13:26:18.478: I/VIEwRootImpl(15088): VIEwRoot's touch Event : touch UP11-05 13:26:18.508: I/Ads(15088): Starting ad request.11-05 13:26:18.578: I/ActivityManager(15088): Timeline: Activity_IDle ID: androID.os.BinderProxy@4205cbf0 time:10733452311-05 13:26:18.648: I/Ads(15088): Not on service, return11-05 13:26:18.748: I/chromium(15088): [INFO:CONSolE(0)] "document was loaded from Application Cache with manifest https://Googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.appcache", source: https://Googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.HTML (0)11-05 13:26:18.748: I/chromium(15088): [INFO:CONSolE(0)] "Application Cache Checking event", source: https://Googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.HTML (0)11-05 13:26:18.758: I/chromium(15088): [INFO:CONSolE(0)] "Application Cache Checking event", source: https://Googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.HTML (0)11-05 13:26:18.878: D/dalvikvm(15088): GC_FOR_ALLOC freed 593K, 29% free 8904K/12400K, paused 19ms, total 19ms11-05 13:26:18.958: I/chromium(15088): [INFO:CONSolE(0)] "Application Cache NoUpdate event", source: https://Googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.HTML (0)11-05 13:26:18.958: I/chromium(15088): [INFO:CONSolE(0)] "Application Cache NoUpdate event", source: https://Googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.HTML (0)11-05 13:26:19.118: W/AwContents(15088): nativeOnDraw Failed; clearing to background color.11-05 13:26:19.248: I/chromium(15088): [INFO:async_pixel_transfer_manager_androID.cc(56)] Async pixel transfers not supported11-05 13:26:19.308: I/chromium(15088): [INFO:async_pixel_transfer_manager_androID.cc(56)] Async pixel transfers not supported11-05 13:26:19.358: I/Ads(15088): Ad finished loading.11-05 13:26:21.348: I/VIEwRootImpl(15088): VIEwRoot's KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_BACK, scanCode=158, MetaState=0, flags=0x48, repeatCount=0, eventTime=107337287, downTime=107337287, deviceid=7, source=0x101 } to com.androID.internal.policy.impl.PhoneWindow$DecorVIEw{41e7ae60 V.E..... R....... 0,0-480,800}11-05 13:26:21.458: I/VIEwRootImpl(15088): VIEwRoot's KeyEvent { action=ACTION_UP, keyCode=KEYCODE_BACK, scanCode=158, MetaState=0, flags=0x48, repeatCount=0, eventTime=107337403, downTime=107337287, deviceid=7, source=0x101 } to com.androID.internal.policy.impl.PhoneWindow$DecorVIEw{41e7ae60 V.E..... R....... 0,0-480,800}11-05 13:26:22.358: I/VIEwRootImpl(15088): VIEwRoot's KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_BACK, scanCode=158, MetaState=0, flags=0x48, repeatCount=0, eventTime=107338296, downTime=107338296, deviceid=7, source=0x101 } to com.androID.internal.policy.impl.PhoneWindow$DecorVIEw{41e7ae60 V.E..... R....... 0,0-480,800}11-05 13:26:22.468: I/VIEwRootImpl(15088): VIEwRoot's KeyEvent { action=ACTION_UP, keyCode=KEYCODE_BACK, scanCode=158, MetaState=0, flags=0x48, repeatCount=0, eventTime=107338412, downTime=107338296, deviceid=7, source=0x101 } to com.androID.internal.policy.impl.PhoneWindow$DecorVIEw{41e7ae60 V.E..... R....... 0,0-480,800}
解决方法:
尝试将广告代码包装在片段中,如下所示:
rootVIEw.post( new Runnable() { @OverrIDe public voID run() { if (MainActivity.mInterstitialAd.isLoaded()) MainActivity.mInterstitialAd.show(); } });
部分广告可能会以某种方式损害您片段的生命周期.
或者您可以尝试使用postDelayed(Runnable runnable,long delay)进行实验;并向广告展示一些适当设置的延迟.也许这会有所帮助.
总结以上是内存溢出为你收集整理的java – 在OnCreateView中显示Admob插页式广告时未加载Android片段布局全部内容,希望文章能够帮你解决java – 在OnCreateView中显示Admob插页式广告时未加载Android片段布局所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)