2015-11-05 78 views
3

我在使用AdMob在我的應用中展示廣告時遇到了一些問題。問題如主題中所述。在OnCreateView中顯示Admob插頁式廣告時Android片段佈局未加載

這是我的出發活動類:

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類中...

我已經花了很多時間試圖解決這個問題,現在我非常絕望,甚至準備了一個示例小型eclipse項目來顯示問題: https://drive.google.com/file/d/0ByRNGNhz-adOQ091OU15c3ZUckE/view?usp=sharing

您必須在strings.xml中更改您的廣告單元ID,並將您的測試設備ID設置爲requestNewInterstitial方法中的MainActivity.java以顯示廣告...(並且可能修復一些鏈接錯誤? - 不確定如果谷歌播放服務將正確鏈接爲你們)

而且我會給誰賞光,以解決問題...

更新1:在我的一個設備 - 三星Galaxy S4 mini - 無論我使用測試廣告還是真實廣告都會出現問題。但似乎在我的另一個設備上的LG - D320n的問題似乎只發生在使用測試廣告時...我現在完全困惑...

更新2:添加完整的logcat的應用程序(沒有任何崩潰在應用程序,但很多消息拋出。關閉adevertisement使用「返回」按鈕)

11-05 13:26:13.018: W/GooglePlayServicesUtil(15088): Google Play services out of date. Requires 8115000 but found 5084034 
11-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 5084034 
11-05 13:26:13.068: E/GooglePlayServicesUtil(15088): GooglePlayServices not available due to error 2 
11-05 13:26:13.108: I/ActivityManager(15088): Timeline: Activity_idle id: [email protected] time:107329054 
11-05 13:26:13.188: I/Ads(15088): Not on service, return 
11-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 52ms 
11-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 supported 
11-05 13:26:14.218: I/chromium(15088): [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported 
11-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 Down 
11-05 13:26:15.898: I/ViewRootImpl(15088): ViewRoot's Touch Event : Touch UP 
11-05 13:26:15.958: D/dalvikvm(15088): GC_FOR_ALLOC freed 2271K, 41% free 7316K/12400K, paused 31ms, total 32ms 
11-05 13:26:16.308: I/ActivityManager(15088): Timeline: Activity_launch_request id:com.example.admobinterstitial time:107332251 
11-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 23ms 
11-05 13:26:16.418: I/chromium(15088): [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported 
11-05 13:26:16.488: I/chromium(15088): [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported 
11-05 13:26:16.738: I/ActivityManager(15088): Timeline: Activity_idle id: [email protected] time:107332684 
11-05 13:26:18.418: I/ViewRootImpl(15088): ViewRoot's Touch Event : Touch Down 
11-05 13:26:18.478: I/ViewRootImpl(15088): ViewRoot's Touch Event : Touch UP 
11-05 13:26:18.508: I/Ads(15088): Starting ad request. 
11-05 13:26:18.578: I/ActivityManager(15088): Timeline: Activity_idle id: [email protected] time:107334523 
11-05 13:26:18.648: I/Ads(15088): Not on service, return 
11-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 19ms 
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: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 supported 
11-05 13:26:19.308: I/chromium(15088): [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported 
11-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} 

回答

5

嘗試包裝你的廣告顯示的代碼片段是這樣的:

rootView.post(
     new Runnable() { 
      @Override 
      public void run() { 
       if (MainActivity.mInterstitialAd.isLoaded()) 
        MainActivity.mInterstitialAd.show(); 
      } 
     } 
); 

這可能是間質性廣告在某種程度上影響到了片段的生命週期。

或者您可以嘗試使用postDelayed(Runnable runnable, long delay);進行實驗,並以適當的延遲時間顯示廣告。也許這會有所幫助。

+2

這解決了這個問題。我真的感到舒緩。萬分感謝。奇怪的是,幾周前我沒有遇到這個問題,並在休息幾周後注意到了這一點......我會盡快給予您獎勵 – Savail

+0

我遇到過與AdMob類似的問題,我有一個問題打開在 http://stackoverflow.com/questions/33595325/admob-interstitial-with-viewpager-issues-after-dismiss 我認爲谷歌已經做了一些adMob,因爲它不會影響所有的廣告和真的不穩定。它以前運行良好。 @Bertak也許你也可以回答我的問題 – dejavu89

2

您使用模擬器來運行代碼或設備只是沒有從應用程序的觀點和我手動退出有後?

switch (isGooglePlayServicesAvailable(this)) { 
    case 2: //out date 
     try { 
      GooglePlayServicesUtil.getErrorDialog(2, this, 0).show(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     break; 
} 

這將提示用戶更新播放服務的更新對話框。

我想你正在運行與較低版本的播放服務庫的應用程序。更新它,然後運行你的代碼。

參考https://groups.google.com/forum/#!topic/google-admob-ads-sdk/A_NpbD6leg0

+0

我更新了我的帖子 – Savail

+0

事實上,我的其中一個設備已經過時了,但另一個設備是最新的,問題仍然存在。無論如何,非常感謝你爲這個筆記,因爲這可能會導致其他很難檢測到未來的錯誤! – Savail

1

通過將插頁式廣告作爲靜態存儲在MainActivity上,您正在晾曬自己。從來沒有做到這一點。

您應該將Fragment中的消息傳遞給其包含的Activity,它應該負責顯示廣告。

否則,你幾乎肯定會開始泄漏內存。

相關問題