lundi 25 septembre 2023

Access field in non public class through reflection

I am trying to access private field mRadialTimePickerModeButton in android.widget.TimePickerClockDelegate

TimePickerClockDelegate is a non public class extending TimePicker.AbstractTimePickerDelegate which implements TimePicker.TimePickerDelegate

I cannot make a new instance of this, I need to modify that field on this particular instance.

I tried accessing it like this:

Field field = TimePickerDialog.class.getDeclaredField("mTimePicker");
field.setAccessible(true);
TimePicker timePicker = (TimePicker) field.get(this);

Field delegateField = TimePicker.class.getDeclaredField("mDelegate");
delegateField.setAccessible(true);
Object delegate = delegateField.get(timePicker);

Field buttonField = delegate.getClass().getDeclaredField("mRadialTimePickerModeButton");
buttonField.setAccessible(true);
ImageButton radialTimePickerModeButton = (ImageButton) buttonField.get(delegate);

Getting: No field mRadialTimePickerModeButton in class Landroid/widget/TimePickerClockDelegate; (declaration of 'android.widget.TimePickerClockDelegate' appears in /system/framework/framework.jar!classes4.dex)





Aucun commentaire:

Enregistrer un commentaire