Skip to content Skip to sidebar Skip to footer

Android Java Unable To Load Class

hi am unable to load class in my android app , i can load basic class but when i Initialize context in that class then am not able to load it public class main { // Initalize c

Solution 1:

You need to create an empty contructor in your Java class:

publicclassmain {
    // Initalize contextContext mContext;

    publicmain(){
    }

    publicmain(Context mContext){
        this.mContext = mContext;
    }
    publicbooleanmain() {
       Log.d("MYLOG", "main() called successfully when there context is not initialized like above");
        // some code here  
    }
}

This will only call the empty constructor, which may not be what you want.

Alternatively, you need to choose the constructor you want and add a parameter to your newInstance call (see related SO question here) like so:

Class[] cArg = newClass[1];
cArg[0] = Context.class;
classToLoad.getDeclaredConstructor(cArg).newInstance(context);

so your non-empty constructor

publicmain(Context mContext)

is called.

Post a Comment for "Android Java Unable To Load Class"