在Android Intent中的putExtra中传递null会导致编译时错误吗?

Cod*_*ior 6 java android

我正试图putExtra(String,String)在我的代码中使用null.正如参数所示,第二个参数可以null是字符串,我可以发送它

this.getIntent().putExtra(AppConstant.TestString, null);
Run Code Online (Sandbox Code Playgroud)

当我使用上面的代码时,它给我错误说:

方法putExtra(String,String)对于Intent类型是不明确的

但它允许我使用:

this.getIntent().putExtra(AppConstant.TestString, "");
Run Code Online (Sandbox Code Playgroud)

请在此赐教我.提前致谢.

Szy*_*mon 17

使用时null,编译器不知道您要使用哪种类型,并且无法确定要使用的方法的重载.

您可以将null转换String为通知编译器您使用的方法:

this.getIntent().putExtra(AppConstant.TestString, (String)null);
Run Code Online (Sandbox Code Playgroud)

或者,您可以创建变量并将其设置为null:

String param = null;
this.getIntent().putExtra(AppConstant.TestString, param);
Run Code Online (Sandbox Code Playgroud)