Android java unable to load class -


hi unable load class in android app , can load basic class when initialize context in class not able load it

public class main {     // initalize context     context mcontext;     public main(context mcontext){         this.mcontext = mcontext;     }     public boolean main() {  log.d("mylog", "main() called when there context not initialized above"); // code here   } } 

my class loading code

try{      final file tmpdir = context.getdir("dex", 0);      final dexclassloader classloader = new dexclassloader(libpath, tmpdir.getabsolutepath(), null, this.getclass().getclassloader());      final class<object> classtoload = (class<object>) classloader.loadclass("com.myproject.test.dumy_class");   // package plus main class      final object myinstance = classtoload.newinstance();       // throwing exception here  }     } catch (exception e) { // exception thrown @ statement :   final object myinstance = classtoload.newinstance();  } 

exception got :

java.lang.instantiationexception: can't instantiate class com.myproject.test.dumy_class; no empty constructor

so please .

you need create empty contructor in java class:

public class main {     // initalize context     context mcontext;      public main(){     }      public main(context mcontext){         this.mcontext = mcontext;     }     public boolean main() {        log.d("mylog", "main() called when there context not initialized above");         // code here       } } 

this call empty constructor, may not want.

alternatively, need choose constructor want , add parameter newinstance call (see related question here) so:

class[] carg = new class[1]; carg[0] = context.class; classtoload.getdeclaredconstructor(carg).newinstance(context); 

so non-empty constructor

public main(context mcontext) 

is called.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -