gpt4 book ai didi

java - android 使用 Intent 与 class.forName

转载 作者:行者123 更新时间:2023-12-02 00:32:10 29 4
gpt4 key购买 nike

我在 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com