如何将字符串转换为类名

Vit*_*sky 3 android class

我正在尝试将字符串转换为类名.我的代码是:

app.actNum = "third.class";
        in.close();
                //on test
                   //  Intent intent = new Intent(this.context,Class.forName(app.actNum));



Intent dialogIntent = new Intent(getBaseContext(), Class.forName(app.actNum));
Run Code Online (Sandbox Code Playgroud)

但得到

  get ClassNotFoundException on Class.forName android
Run Code Online (Sandbox Code Playgroud)

Ade*_*ros 28

在调用时Class.forName(),您应该使用没有扩展名的类名.因此,如果您的被调用Test并且在包中 mypackage.tests,那么您应该使用:

Class.forName("mypackage.tests.Test")
Run Code Online (Sandbox Code Playgroud)