rosu alin
rosu alin

Reputation: 5830

Android conversion to Kotlin. How to call inner method in constructor super

This is the method I have:

private static class AvatarNotFound
        extends Throwable
{
    public AvatarNotFound(String message, @NonNull AddressableAvatarView addressableAvatarView)
    {
        super(message + getErrorMessageSuffix(addressableAvatarView));
    }

    private static String getErrorMessageSuffix(@NonNull AddressableAvatarView addressableAvatarView)
    {
        return ". Addressable: " + addressableAvatarView.toString();
    }
}

converting to Kotlin it returns this code, which will become this:

   private open class AvatarNotFound(message: String, addressableAvatarView: AddressableAvatarView) : Throwable(message + getErrorMessageSuffix(addressableAvatarView)) {

    private fun getErrorMessageSuffix(addressableAvatarView: AddressableAvatarView): String {
        return ". Addressable: $addressableAvatarView"
    }
   }

But it says that getErrorMessageSuffix is an unresolved reference? How can I make the constructor recognise it?

Upvotes: 0

Views: 55

Answers (2)

Maulik Togadiya
Maulik Togadiya

Reputation: 591

try this may help you:

companion object{
        private fun getErrorMessageSuffix(addressableAvatarView: AddressableAvatarView): String {
            return ". Addressable: " + addressableAvatarView.toString()
        }
    }
}

Upvotes: 1

Abhay Mathur
Abhay Mathur

Reputation: 1

You can use companion object here, which will enable to access your method with class name.

companion object
    {
        private fun getErrorMessageSuffix(addressableAvatarView: AddressableAvatarView): String 
         {
            return ". Addressable: $addressableAvatarView"
        }
    }

Upvotes: 0

Related Questions