android Class.forName抛出异常

时间:2012-10-10 03:48:29

标签: java android class reflection

我在Android项目中玩Java反射。

源代码:

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");
  • 以上代码位于我活动的onCreate()中。
  • 该项目的构建目标是Android 4.0。
  • 我在使用Android 4.0.4的设备上运行它。

结果: 变量unusedVar不是null,但Class.forName会抛出ClassNotFoundException

日志:

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

为什么?是因为android.view.View.OnSystemUiVisibilityChangeListener接口而不是吗?

1 个答案:

答案 0 :(得分:1)

接口OnSystemUiVisibilityChangeListener嵌套在课程View中,所以我相信您必须Class.forName("android.view.View$OnSystemUiVisibilityChangeListener");(请注意'$')。

参考文献: