且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将字符串转换为类名

更新时间:2023-02-23 18:50:40

致电时的Class.forName(),你应该使用类名不带扩展名。所以,如果您的被称为测试并在 mypackage.tests ,那么你应该使用:

 的Class.forName(mypackage.tests.Test)

I am trying to convert string to class name. My code is:

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));

but get

  get ClassNotFoundException on Class.forName android

When calling Class.forName(), you should use the class's name without the extension. So if your class is calledTest and is in the package mypackage.tests, then you should use:

Class.forName("mypackage.tests.Test")