gpt4 book ai didi

java - "Class"无法创建对象

转载 作者:行者123 更新时间:2023-12-02 00:16:44 24 4
gpt4 key购买 nike

我正在创建一个 Class 对象以启动一个新 Activity 。但问题是当我输入时

Class ourclass = new Class.__

它无法识别该类,并且我无法使用父类(super class)“Class”的方法。它说类是原始类型。我应该将它分配给什么类型,因为我想使用“forName()”方法来传递类名。这是代码

    protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
try
{
Class ourclass =Class.__ // The suggestions for static functions doesn't popup, and I get errors
//Intent myintent = new Intent();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}

}
}

问候。

最佳答案

What type should I assign it to, because I want to use "forName()" method in which I want to pass a class name.

Class.forName是一个静态方法 - 您不会在实例上调用它:

Class<?> ourClass = Class.forName("foo.bar.Baz");

(启动 Activity 时这是否确实合适是另一回事,但这个答案只是解决了 Class.forName 部分...)

关于java - "Class"无法创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11775399/

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