0

In my app, I have implemented a Token class as a singleton to handle user's identification. However, when I launch my app, it fails with the following error:

java.lang.RuntimeException: Unable to instantiate application appliflo.Token: java.lang.IllegalAccessException: private appliflo.Token() is not accessible from class android.app.Instrumentation

I am wondering weather this could be due to the Application class inheritance.

Here is my singleton:

package appliflo;
import android.app.Application;

public final class Token extends Application{

    private static long TokenID;
    private static Token instance;

    private Token(){

    }

    private Token(long TokenID){    
        this.TokenID = TokenID;
    }

    public static synchronized Token getToken() { 
        if (instance == null) instance = new Token();
        return instance;   
    }

    public static synchronized void setToken(long id){
        if (instance == null) instance = new Token(id);
        instance.TokenID = id; 
    }

    public long getTokenID(){    
        return this.TokenID;
    }

}
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
floflo29
  • 2,261
  • 2
  • 22
  • 45

1 Answers1

0

As your exception message states the class android.app.Instrumentation cannot create an instance of your application because it cannot access its constructor since it is private.

You need to have a public constructor that has no parameters in order to allow the Android system to create an instance of it to start your application.

You will not be creating an instance of Token yourself so the static methods getToken and setToken will not be useful.

Update for question in comment:

If you need to update the tokenId update your get and set methods to only update the long and not create a new instance of your Token class.

public static synchronized long getToken() {
    return TokenID;
}

public static synchronized void setToken(long id){
    TokenID = id;
}
George Mulligan
  • 11,813
  • 6
  • 37
  • 50
  • But if I want to modify the variable TokenID (every time the login is modified), I will need to use the setToken id method no? – floflo29 Jan 17 '16 at 16:29
  • Technically speaking, can my Token class be still considered as a singleton (considering your update)? – floflo29 Jan 17 '16 at 16:47
  • Yes there should only ever be one instance of your application class at a time created for you by the Android system. You can set things up to keep a static reference to it but there are still some things you need to be aware of as explained [here](http://stackoverflow.com/questions/2002288/static-way-to-get-context-on-android) and [here](http://stackoverflow.com/questions/987072/using-application-context-everywhere) – George Mulligan Jan 17 '16 at 16:57
  • Do I need my Token instance anymore? – floflo29 Jan 17 '16 at 18:04
  • Not if all you need is the `TokenID` from it. – George Mulligan Jan 17 '16 at 18:16
  • However, I do not see what can prevent multiple Token instance in my app, and how to keep track of the unique instance along my app... – floflo29 Jan 17 '16 at 19:00
  • It seems that my first approach should work according to this post: http://stackoverflow.com/questions/16517702/singleton-in-android – floflo29 Jan 17 '16 at 20:45
  • That will not work again because you do not control the creation of the Application. Android creates the Application for you in the `android.app.Instrumentation` class listed in your exception using `Application app = (Application)clazz.newInstance();`. Look up the documentation on `newInstance` and you will find it uses the default no args constructor. Since you made the default constructor private the `Instrumentation` class cannot access it and a `IllegalAccessException` is thrown. – George Mulligan Jan 17 '16 at 21:49
  • Do you understand now? – George Mulligan Jan 17 '16 at 23:03