This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Access issues in an Android app

2 replies
Nolan Darilek
Joined: 2009-10-11,
User offline. Last seen 42 years 45 weeks ago.

Hi, all. I'm having issues calling into the Android API from Scala, and
suspect that perhaps the accessibility rules for variables may be
conflicting somehow, but I'm not sure.

I'm writing code that performs different contact lookups for different
Android API versions, since how contacts are looked up changed in V5.
Here's the code I have:

val column = if(sdkVersion >= Build.VERSION_CODES.ECLAIR)
ContactsContract.PhoneLookup.DISPLAY_NAME
else
Contacts.PeopleColumns.DISPLAY_NAME
name = cursor.getString(cursor.getColumnIndex(column))

I get the following with this code:

[error]
/home/nolan/Projects/Spiel/src/main/scala/info/spielproject/spiel/telephony.scala:34:
value DISPLAY_NAME is not a member of object
android.provider.ContactsContract.PhoneLookup
[error] ContactsContract.PhoneLookup.DISPLAY_NAME
[error] ^
[error] one error found

The API docs here seem to suggest that this should work, though:

http://developer.android.com/reference/android/provider/ContactsContract...

Now, when I chase DISPLAY_NAME through the javadoc, I see that it is
actually defined in ContactsContract.ContactsColumns:

http://developer.android.com/reference/android/provider/ContactsContract...

which is declared as a protected interface. This would seem to explain
why I can't access this constant on PhoneLookup, though I don't see why
it should work in Java and not in Scala.

Compare this to Contacts.PeopleColumns, the method for Android 1.5 and
below which at least compiles:

http://developer.android.com/reference/android/provider/Contacts.PeopleC...

which is a public static interface.

So am I doing something wrong? Is the PhoneLookup javadoc wrong, and
should I not be able to access DISPLAY_NAME there? Or is there some
Scala trick I need to use to access this constant?

Here's another tutorial that uses Contacts.DISPLAY_NAME. This doesn't
seem to work in Scala either, same error:

http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contact...

So it looks like I'm doing this correctly according to several sources,
but it just isn't working at all.

Thanks.

Lex
Joined: 2010-02-28,
User offline. Last seen 42 years 45 weeks ago.
Re: Access issues in an Android app

I remember reading somewhere that Scala cannot access >protected
static< members. This is a know limitation of interoperability with
java, there is a ticked on track with the consensus it wont be fixed.
You could try to work around by writing a java class that exposes the
required protected static members and access them via your wrapper.

On Wed, Mar 10, 2010 at 5:45 PM, Nolan Darilek wrote:
> Hi, all. I'm having issues calling into the Android API from Scala, and
> suspect that perhaps the accessibility rules for variables may be
> conflicting somehow, but I'm not sure.
>
> I'm writing code that performs different contact lookups for different
> Android API versions, since how contacts are looked up changed in V5. Here's
> the code I have:
>
>          val column = if(sdkVersion >= Build.VERSION_CODES.ECLAIR)
>            ContactsContract.PhoneLookup.DISPLAY_NAME
>          else
>            Contacts.PeopleColumns.DISPLAY_NAME
>          name = cursor.getString(cursor.getColumnIndex(column))
>
> I get the following with this code:
>
> [error]
> /home/nolan/Projects/Spiel/src/main/scala/info/spielproject/spiel/telephony.scala:34:
> value DISPLAY_NAME is not a member of object
> android.provider.ContactsContract.PhoneLookup
> [error]             ContactsContract.PhoneLookup.DISPLAY_NAME
> [error]                                          ^
> [error] one error found
>
> The API docs here seem to suggest that this should work, though:
>
> http://developer.android.com/reference/android/provider/ContactsContract...
>
> Now, when I chase DISPLAY_NAME through the javadoc, I see that it is
> actually defined in ContactsContract.ContactsColumns:
>
> http://developer.android.com/reference/android/provider/ContactsContract...
>
> which is declared as a protected interface. This would seem to explain why I
> can't access this constant on PhoneLookup, though I don't see why it should
> work in Java and not in Scala.
>
> Compare this to Contacts.PeopleColumns, the method for Android 1.5 and below
> which at least compiles:
>
> http://developer.android.com/reference/android/provider/Contacts.PeopleC...
>
> which is a public static interface.
>
> So am I doing something wrong? Is the PhoneLookup javadoc wrong, and should
> I not be able to access DISPLAY_NAME there? Or is there some Scala trick I
> need to use to access this constant?
>
> Here's another tutorial that uses Contacts.DISPLAY_NAME. This doesn't seem
> to work in Scala either, same error:
>
> http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contact...
>
> So it looks like I'm doing this correctly according to several sources, but
> it just isn't working at all.
>
> Thanks.
>
>
>

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Access issues in an Android app
Indeed!
http://lampsvn.epfl.ch/trac/scala/ticket/1806
And it is in the Java interoperability FAQ as well!
http://www.scala-lang.org/faq/4

On Wed, Mar 10, 2010 at 9:33 PM, Lex <lexn82@gmail.com> wrote:
I remember reading somewhere that Scala cannot access >protected
static< members. This is a know limitation of interoperability with
java, there is a ticked on track with the consensus it wont be fixed.
You could try to work around by writing a java class that exposes the
required protected static members and access them via your wrapper.

On Wed, Mar 10, 2010 at 5:45 PM, Nolan Darilek <nolan@thewordnerd.info> wrote:
> Hi, all. I'm having issues calling into the Android API from Scala, and
> suspect that perhaps the accessibility rules for variables may be
> conflicting somehow, but I'm not sure.
>
> I'm writing code that performs different contact lookups for different
> Android API versions, since how contacts are looked up changed in V5. Here's
> the code I have:
>
>          val column = if(sdkVersion >= Build.VERSION_CODES.ECLAIR)
>            ContactsContract.PhoneLookup.DISPLAY_NAME
>          else
>            Contacts.PeopleColumns.DISPLAY_NAME
>          name = cursor.getString(cursor.getColumnIndex(column))
>
> I get the following with this code:
>
> [error]
> /home/nolan/Projects/Spiel/src/main/scala/info/spielproject/spiel/telephony.scala:34:
> value DISPLAY_NAME is not a member of object
> android.provider.ContactsContract.PhoneLookup
> [error]             ContactsContract.PhoneLookup.DISPLAY_NAME
> [error]                                          ^
> [error] one error found
>
> The API docs here seem to suggest that this should work, though:
>
> http://developer.android.com/reference/android/provider/ContactsContract.PhoneLookup.html
>
> Now, when I chase DISPLAY_NAME through the javadoc, I see that it is
> actually defined in ContactsContract.ContactsColumns:
>
> http://developer.android.com/reference/android/provider/ContactsContract.ContactsColumns.html#DISPLAY_NAME
>
> which is declared as a protected interface. This would seem to explain why I
> can't access this constant on PhoneLookup, though I don't see why it should
> work in Java and not in Scala.
>
> Compare this to Contacts.PeopleColumns, the method for Android 1.5 and below
> which at least compiles:
>
> http://developer.android.com/reference/android/provider/Contacts.PeopleColumns.html
>
> which is a public static interface.
>
> So am I doing something wrong? Is the PhoneLookup javadoc wrong, and should
> I not be able to access DISPLAY_NAME there? Or is there some Scala trick I
> need to use to access this constant?
>
> Here's another tutorial that uses Contacts.DISPLAY_NAME. This doesn't seem
> to work in Scala either, same error:
>
> http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/4/
>
> So it looks like I'm doing this correctly according to several sources, but
> it just isn't working at all.
>
> Thanks.
>
>
>



--
Daniel C. Sobral

I travel to the future all the time.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland