Search code examples
androidclasskotlinconstructorsuper

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?


Solution

  • try this may help you:

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