-5

I am playing with Java reflection in an Android project.

Source code:

Log.d("LOG", "begin");
try {
    final android.view.View.OnSystemUiVisibilityChangeListener unusedVar = new android.view.View.OnSystemUiVisibilityChangeListener() {

        @Override
        public void onSystemUiVisibilityChange(int visibility) {
            // TODO Auto-generated method stub

        }
    };
    Log.d("LOG", "unusedVar is null = " + (unusedVar == null? "Yes": "No"));
    Class newClass = Class.forName("android.view.View.OnSystemUiVisibilityChangeListener");
    Log.d("LOG", "newClass created");
} catch (Exception ex) {
    Log.d("LOG", "Failed to create newClass", ex);
}
Log.d("LOG", "end");
  • The above code is in onCreate() of my activity.
  • The project's build target is Android 4.0.
  • I am running it on a device with Android 4.0.4.

Result: The variable unusedVar is NOT null, but Class.forName throws a ClassNotFoundException.

Log:

D/LOG(13205): begin
D/LOG(13205): unusedVar is null = No
D/LOG(13205): Failed to create newClass
D/LOG(13205): java.lang.ClassNotFoundException: android.view.View.OnSystemUiVisibilityChangeListener
D/LOG(13205):   at java.lang.Class.classForName(Native Method)
D/LOG(13205):   at java.lang.Class.forName(Class.java:217)
D/LOG(13205):   at java.lang.Class.forName(Class.java:172)
D/LOG(13205):   at com.MyActivity.onCreate(VideoPlayerActivity.java:100)
D/LOG(13205):   at android.app.Activity.performCreate(Activity.java:4465)
D/LOG(13205):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
D/LOG(13205):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
D/LOG(13205):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
D/LOG(13205):   at android.app.ActivityThread.access$600(ActivityThread.java:123)
D/LOG(13205):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
D/LOG(13205):   at android.os.Handler.dispatchMessage(Handler.java:99)
D/LOG(13205):   at android.os.Looper.loop(Looper.java:137)
D/LOG(13205):   at android.app.ActivityThread.main(ActivityThread.java:4424)
D/LOG(13205):   at java.lang.reflect.Method.invokeNative(Native Method)
D/LOG(13205):   at java.lang.reflect.Method.invoke(Method.java:511)
D/LOG(13205):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
D/LOG(13205):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
D/LOG(13205):   at dalvik.system.NativeStart.main(Native Method)
D/LOG(13205): Caused by: java.lang.NoClassDefFoundError: android/view/View/OnSystemUiVisibilityChangeListener
D/LOG(13205):   ... 18 more
D/LOG(13205): Caused by: java.lang.ClassNotFoundException: android.view.View.OnSystemUiVisibilityChangeListener
D/LOG(13205):   at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
D/LOG(13205):   at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
D/LOG(13205):   at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
D/LOG(13205):   ... 18 more
D/LOG(13205): end

Why is that? Is it because android.view.View.OnSystemUiVisibilityChangeListener is an interface rather than a class?

Pang
  • 9,564
  • 146
  • 81
  • 122
  • 1
    you may want to use java.lang.reflect.Proxy. take a look at this: http://stackoverflow.com/questions/1082850/java-reflection-create-an-implementing-class – Naytzyrhc Oct 10 '12 at 04:04

1 Answers1

2

Interface OnSystemUiVisibilityChangeListener is nested in class View, so I believe you would have to do Class.forName("android.view.View$OnSystemUiVisibilityChangeListener"); (note the '$').

References: