我试图按照此处描述的内容: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));
正在发生的事情是上面NullPointerException
行的Log
,因为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
我为Google代码管理器启用了日志记录,我觉得我看起来像#34;正常&#34;行为:
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.
并且我的onResult
中的日志语句也从未显示过,这可以解释为什么container
为空,因为这是任何初始化发生的唯一位置。不应该onResult
到达并container
被初始化吗?我对如何正确实施这一点感到有些困惑。
答案 0 :(得分:1)
加载ContainerHolder
是异步发生的,因此在您当前尝试记录值时,尚未为其分配值。
您关联的网站包含以下关于onResult()
的文档。
只要满足下列条件之一,就会调用onResult方法 发生的情况:
- 已加载已保存的容器
- 如果没有保存的容器,则加载网络容器
- 发生指定的超时
醇>
这意味着在调用container
之后才能访问onResult(...)
,因为这是为其分配值。您可以将日志语句移动到该方法中以测试检索值,并在此时执行您需要对容器执行的任何其他操作。
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));
}