我在 ArrayList 中有类的字符串名称。
如果我尝试像这样调用 Intent:
Intent intent = new Intent(context, MultipleChoice.class);
context.startActivity(intent);
它工作没有问题,但是当我想这样做时,我得到一个ClassFoundException
。
在 config.getTests(config.getPage()))
中,我得到 "MultipleChoice"
返回。
try {
Intent intent = new Intent(context, Class.forName(config.getTests(config.getPage())));
context.startActivity(intent);
context.finish();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
为什么会这样?
最佳答案
我认为config.getTests(....)
类名应带包,例如 com.xyz.MultipleChoice
。您可以尝试将其硬编码在 Class.forName(..)
中第一的。如果有效的话,你可以想办法从 config.getTest(....)
获取它
关于java - android 使用 Intent 与 class.forName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8745333/