jeudi 18 novembre 2021

Calling function by string name in activity

I trying to call function by names as string:

Method method = null;
            try {
                method = 
Class.forName("com.lab.android.TabActivity").getMethod(item,String.class);
                method.invoke(this, null);
            } catch (NoSuchMethodException e) {
                Log.e("DTAG","NoSuchMethodException "+e.getMessage());
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                Log.e("DTAG","ClassNotFoundException "+e.getMessage());
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                Log.e("DTAG","IllegalAccessException "+e.getMessage());
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                Log.e("DTAG","InvocationTargetException "+e.getMessage());
                e.printStackTrace();
            }

I get an exeptioon:

NoSuchMethodException com.lab.android.TabActivity.somesome [class java.lang.String]

This is the function in my ativity:

public static void somesome() {
    Log.d("DTAG","Great Success");
}




Aucun commentaire:

Enregistrer un commentaire