Question

I am on Android 2.2 SDK and could not get my static block inside MultiUserChat class to execute. I have tried to force load it as

try 
{
    String qual = MultiUserChat.class.getName();
    ClassLoader.getSystemClassLoader().loadClass(qual);
        
} catch (ClassNotFoundException e) {

    e.printStackTrace();
}

and it always hits the catch block. 'qual' gets the valid name of the class... what can it be?

Was it helpful?

Solution

Your app includes both framework classes like ArrayList and Activity, plus application classes like FlashlightActivity. The framework classes are loaded by the system class loader (and also the bootstrap class loadeR); the application classes are loaded by the application class loader.

The system class loader can only see the system classes. It doesn't know the application class path and it can't be used to load application classes. You need to use the application class loader to do that. The easiest way to get a reference to the application class loader is via an application class:

try {
    String qual = MultiUserChat.class.getName();
    MyActivity.class.getClassLoader().loadClass(qual);
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top