将String转换为类?

时间:2015-10-28 05:25:23

标签: java android class android-intent

我有一个名为theClass的字符串。它包含一个.class的字符串。我想把那个字符串放到我意图的第二个参数中;我知道这是不可能的,但是如何在意图中使用我的字符串,这是一个类?

例如,如果String theClass是:

String theClass = "anotherClassInMyProj.class";

我做不了类似的事情:

Intent i = new Intent(this, theClass);

我如何将字符串用作类?我是否需要以某种方式转换它,并且有内置方法吗?

3 个答案:

答案 0 :(得分:1)

你需要使用:

String theClass = "anotherClassInMyProj.class"
Class c=Class.forName(theClass);

答案 1 :(得分:1)

试试这个:

    String className = "com.example.anotherClassInMyProj";
    Intent startfunction = new Intent();
    startfunction.setClassName(getPackageName().toString(), classname);
    startActivity(startfunction);

答案 2 :(得分:1)

Intent i= new Intent(this,Class.forName(theClass);