次の問題があります: クラスがあり、リフレクションを使用してその独自の保護されたメソッドの 1 つを呼び出そうとしていますが、例外が発生しています: java.lang.IllegalAccessException: メソッドへのアクセスが拒否されました
誰かがこれに光を当てることができますか?
基本クラス:
public abstract class BaseReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// invoke the correct event method:
Method method;
try {
method = this.getClass().getDeclaredMethod("testMethod");
method.invoke(this);
} catch (Throwable ex) {
// ... display exception message
}
}
protected void testMethod() {
}
}
派生具象クラス:
class MyReceiver extends BaseReceiver {
@Override
protected void testMethod() {
// display error message
}
}