2016-05-16 63 views
1

我試圖遵循什麼這裏描述:https://developers.google.com/tag-manager/android/v4/#init,這是我所:谷歌代碼管理工具容器空

TagManager tagManager = TagManager.getInstance(this); 
    tagManager.setVerboseLoggingEnabled(true); 


    PendingResult<ContainerHolder> pending = 
      tagManager.loadContainerPreferNonDefault("GTM-XXXXXX", 
        R.raw.gtm); 


    pending.setResultCallback(new ResultCallback<ContainerHolder>() { 
     @Override 
     public void onResult(ContainerHolder containerHolder) { 
      ContainerHolderSingleton.setContainerHolder(containerHolder); 
      container = containerHolder.getContainer(); 
      Log.d("NICK","Inside onResult...") ; 
      if (!containerHolder.getStatus().isSuccess()) { 
       Log.e("CuteAnimals", "failure loading container"); 

       return; 
      } 

      ContainerLoadedCallback.registerCallbacksForContainer(container); 
      containerHolder.setContainerAvailableListener(new ContainerLoadedCallback()); 

     } 
    }, 3000, TimeUnit.MILLISECONDS); 


    Log.d("NICK","Value: "+container.getString("test_value)); 

和正在發生的事情是,在上述LogNullPointerException因爲container爲空:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nick.app/com.nick.app.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.android.gms.tagmanager.Container.getString(java.lang.String)' on a null object reference 
                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                    at android.os.Looper.loop(Looper.java:148) 
                    at android.app.ActivityThread.main(ActivityThread.java:5422) 
                    at java.lang.reflect.Method.invoke(Native Method) 
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.android.gms.tagmanager.Container.getString(java.lang.String)' on a null object reference 

我啓用了日誌記錄的谷歌標籤管理器,我得到了什麼,在我看來是「正常」的行爲:

05-16 11:53:04.865 22585-22662/com.nick.app V/GoogleTagManager: Deleted 0 expired items 
05-16 11:53:04.867 22585-22585/com.nick.app V/GoogleTagManager: Attempting to load a container from the resource ID 2131034112 (com.nick.app:raw/gtm) 
05-16 11:53:04.888 22585-22585/com.nick.app V/GoogleTagManager: The container was successfully loaded from the resource (using JSON file format) 
05-16 11:53:04.903 22585-22664/com.nick.app V/GoogleTagManager: Attempting to load resource from disk 
05-16 11:53:04.925 22585-22664/com.nick.app V/GoogleTagManager: loadAfterDelay: containerId=GTM-XXXXXXX delay=40721307 
05-16 11:53:04.925 22585-22664/com.nick.app D/GoogleTagManager: Setting previous container version: NQ$0 
05-16 11:53:04.929 22585-22664/com.nick.app V/GoogleTagManager: The Disk resource was successfully read. 

而且我的Log Log onResult裏面也沒有顯示,這可以解釋爲什麼container爲空,因爲這是任何初始化發生的唯一位置。不應該onResultcontainer被初始化?我有點困惑,應該如何正確實施。

回答

1

加載ContainerHolder是異步發生的,所以在您當前正試圖從中記錄值的位置沒有分配值。

您鏈接的網站有關於onResult()的以下文檔。

的onResult方法將被立即調用爲以下 情況之一:

  1. 保存的容器裝
  2. 如果沒有保存容器,網絡集裝箱被裝載
  3. 發生指定的超時時間

This m意味着直到onResult(...)被調用之後才能訪問container,因爲那是指定值的位置。您可以將日誌語句移動到該方法中,以測試檢索值並在該點處執行任何其他與容器相關的操作。

public void onResult(ContainerHolder containerHolder) { 
    ContainerHolderSingleton.setContainerHolder(containerHolder); 
    container = containerHolder.getContainer(); 

    Log.d("NICK","Inside onResult...") ; 
    if (!containerHolder.getStatus().isSuccess()) { 
     Log.e("CuteAnimals", "failure loading container"); 
     return; 
    } 

    ContainerLoadedCallback.registerCallbacksForContainer(container); 
    containerHolder.setContainerAvailableListener(new ContainerLoadedCallback()); 

    // do whatever you want with the container at this point 
    Log.d("NICK","Value: " + container.getString("test_value)); 
} 
相關問題