dimanche 24 juin 2018

Android reflection can't find constructor

I have that class:

public class DNDRunner {
    private NotificationManager mNoMan;

    public DNDRunner(Context context) {
        mNoMan = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }

    public void run(String param) {
        mNoMan.setZenMode(Integer.parseInt(param), null, "DNDRunner");
    }
}

And i call run method by reflection using:

try {
    Class mRunner = Class.forName(runner);
    Constructor constructor = mRunner.getConstructor(new Class[]{Context.class});
    Object object = constructor.newInstance(new Object[]{mContext});
    Method run = mRunner.getMethod("run", new Class[]{String.class});
    run.invoke(object, new Object[]{value});
} catch (Exception e) {
    Log.e(TAG, "Runner", e);
}

but i get:

java.lang.NoSuchMethodException: <init> [class android.content.Context]
at java.lang.Class.getConstructor0(Class.java:2320)
at java.lang.Class.getConstructor(Class.java:1725)

what i'm doing wrong? the constructor is obviously there





Aucun commentaire:

Enregistrer un commentaire