lundi 20 juillet 2015

Launch intent inside method run through reflection

Inside the Configuration activity I have two controls that are calling the same method. Here is the method:

public void goDetection() {     
        Intent detection = new Intent(this, Detection.class);
        startActivity(detection);
    }

This method is executed from a basic button click and it works fine. The same method should also be executed inside an AlertDialog, as method associated to the "positiveButton". I invoke this method to the "positiveButton"'s onClick handler as this:

try {
    Method method = Configuration.class.getMethod(methodPositive);
    Object obj = new Configuration(); 
    method.invoke(obj);
} catch (NoSuchMethodException e) {
    Log.d("There is an error in Configuration class: NoSuchMethodException", e.getMessage());
} catch (IllegalAccessException e) {
    Log.d("There is an error in Configuration class: IllegalAccessException", e.getMessage());
} catch (IllegalArgumentException e) {
    Log.d("There is an error in Configuration class: IllegalArgumentException", e.getMessage());
} catch (InvocationTargetException e) {
    Log.d("There is an error in Configuration class: InvocationTargetException", e.getMessage());
}

where methodPositive = "goDetection". Using a Log.d() insertion in the goDetection() method I find that the method gets started, but there is an error when executing the

startActivity(detection);

statement. From LogCat the first application-code-related error is "NullPointerException: println needs a message" in the above statement:

catch (InvocationTargetException e) {
    Log.d("There is an error in Configuration class: InvocationTargetException", e.getMessage());
}

It's not the first time I use this mechanism but it's the first time I get stuck. What am I missing? Many thanks!





Aucun commentaire:

Enregistrer un commentaire