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));
和正在發生的事情是,在上述Log
線NullPointerException
因爲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
爲空,因爲這是任何初始化發生的唯一位置。不應該onResult
和container
被初始化?我有點困惑,應該如何正確實施。