Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing null in putExtra in Android Intent giving compile time error?

Tags:

java

android

I'm trying to use putExtra(String,String) in my code to pass null. As the parameter suggests the second parameter can be null as it is a string and I can send it

this.getIntent().putExtra(AppConstant.TestString, null);

When i use the above code it gives me error saying :

The method putExtra(String, String) is ambiguous for the type Intent

However it allows me to use :

this.getIntent().putExtra(AppConstant.TestString, "");

Kindly enlighten me on this. Thanks in advance.

like image 228
CodeWarrior Avatar asked Nov 13 '13 11:11

CodeWarrior


1 Answers

When you use null, the compiler doesn't know which is the type you want to use and cannot decide which overload of the method to use.

You can cast your null to String to inform compiler which method you use:

this.getIntent().putExtra(AppConstant.TestString, (String)null);

Alternatively, you can create a variable and set it to null:

String param = null;
this.getIntent().putExtra(AppConstant.TestString, param);
like image 103
Szymon Avatar answered Oct 25 '22 01:10

Szymon