我正在尝试通过此链接实施GA Campaign Measurement https://developers.google.com/analytics/devguides/collection/android/v2/campaigns#campaign-params
所以,我已经完成了所有工作,但它在我的代码中提供了NullpointerException
。
// Get the intent that started this Activity.
Intent installIntent = this.getIntent();
Uri Installuri = installIntent.getData();
EasyTracker.getInstance().setContext(this);
if (installIntent != null) {
EasyTracker.getTracker().setCampaign(Installuri.getPath()); //line 82
}
这是代码,应该执行常规跟踪。没什么特别的。但是在第82行,如上所述,它会抛出NullPointerException。这是logcat:
06-23 19:54:54.864: E/AndroidRuntime(13773): at android.os.Looper.loop(Looper.java:137)
06-23 19:54:54.864: E/AndroidRuntime(13773): at android.app.ActivityThread.main(ActivityThread.java:4441)
06-23 19:54:54.864: E/AndroidRuntime(13773): at java.lang.reflect.Method.invokeNative(Native Method)
06-23 19:54:54.864: E/AndroidRuntime(13773): at java.lang.reflect.Method.invoke(Method.java:511)
06-23 19:54:54.864: E/AndroidRuntime(13773): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-23 19:54:54.864: E/AndroidRuntime(13773): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-23 19:54:54.864: E/AndroidRuntime(13773): at dalvik.system.NativeStart.main(Native Method)
06-23 19:54:54.864: E/AndroidRuntime(13773): Caused by: java.lang.NullPointerException
06-23 19:54:54.864: E/AndroidRuntime(13773): at uk.co.futurelite.elite.drum.machine.Main.onCreate(Main.java:82)
06-23 19:54:54.864: E/AndroidRuntime(13773): at android.app.Activity.performCreate(Activity.java:4465)
06-23 19:54:54.864: E/AndroidRuntime(13773): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-23 19:54:54.864: E/AndroidRuntime(13773): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
06-23 19:54:54.864: E/AndroidRuntime(13773): ... 11 more
所以,如果它不是IF
,我会用null
语句检查它:
if (installIntent != null) {
那么,它会抛出NullPointerException会发生什么?当它通过IF
语句时。
PS。我确实完成了它直到结束,虽然我可能会把一些东西传递给broadcastReceiver,但我可以在logcat中看到它成功收到广告系列,如下所示:
06-23 19:54:56.544: W/GAV2(13773): Thread[GAThread,5,main]: Service unavailable (code=1), will retry.
06-23 19:54:56.824: I/GAV2(13773): Thread[GAThread,5,main]: Campaign found: utm_source%3Dxdebugx%26utm_medium%3Dbutton
但应用程序本身崩溃并且无法启动。
答案 0 :(得分:1)
在尝试调用.getPath()之前,需要检查URI是否为null。在您提到的文档中,它是这样写的:
if (intent.getData() != null) {
EasyTracker.getTracker().setCampaign(uri.getPath());
}
使用代码段中的样式,它应如下所示:
// Get the intent that started this Activity.
Intent installIntent = this.getIntent();
Uri Installuri = installIntent.getData();
EasyTracker.getInstance().setContext(this);
if (Installuri != null) {
EasyTracker.getTracker().setCampaign(Installuri.getPath()); //line 82
}