我正在尝试将字符串转换为类名。 我的代码是:
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));
但是
get ClassNotFoundException on Class.forName android
答案 0 :(得分:27)
调用Class.forName()
时,应使用不带扩展名的类名。因此,如果您的类被称为Test
且位于包 mypackage.tests
中,那么您应该使用:
Class.forName("mypackage.tests.Test")
答案 1 :(得分:0)
我认为你在路上遇到了一些问题。
首先,尝试获得完整的路径:
String completePath = PathForClass.class.getResource(app.actNum).toString();
然后,您所要做的就是像以前一样调用它:
Class.forName(completePath.replace(".class","")
(如其他答案中所述,您必须删除.class
。)