Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试使用字符串变量和Class.forName启动活动时,会不断抛出ClassNotFound帮助,请_Java_Android_Class_Android Intent - Fatal编程技术网

Java 尝试使用字符串变量和Class.forName启动活动时,会不断抛出ClassNotFound帮助,请

Java 尝试使用字符串变量和Class.forName启动活动时,会不断抛出ClassNotFound帮助,请,java,android,class,android-intent,Java,Android,Class,Android Intent,嘿,我正试图使用一个字符串变量从这个开始一个新的活动,但每次我这样做都没有发生任何事情。我假设程序只是抛出类not found异常。Class.forName行有问题。请查看我的代码和帮助。我创建的唯一活动是StartingPoint活动,我知道示例活动不起作用,但当我选择StartingPoint时,它甚至不起作用。当我将意图行更改为“…new intent(MenuActivity.this,start point.class)”时,它仍然不起作用,所以我假设它在class.forName(

嘿,我正试图使用一个字符串变量从这个开始一个新的活动,但每次我这样做都没有发生任何事情。我假设程序只是抛出类not found异常。Class.forName行有问题。请查看我的代码和帮助。我创建的唯一活动是StartingPoint活动,我知道示例活动不起作用,但当我选择StartingPoint时,它甚至不起作用。当我将意图行更改为“…new intent(MenuActivity.this,start point.class)”时,它仍然不起作用,所以我假设它在class.forName()行中已经抛出了异常,因为当我将该意图(MenuActivity.this,start point.class)”放在try块之外时,它就起作用了

package com.example.music;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MenuActivity extends ListActivity {

String[] classes = { "StartingPoint", "example1","example2", "example3",
        "example4", "example5", "example6"};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(MenuActivity.this,     
android.R.layout.simple_list_item_1, classes));
}




@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    String cheese = classes[position];
    try{
    Class<?> ourClass = Class.forName(cheese);
    Intent ourIntent = new Intent(MenuActivity.this, ourClass);
    startActivity(ourIntent);

    }catch(ClassNotFoundException e) {
        e.printStackTrace();

    }


}

}
package com.example.music;
导入android.os.Bundle;
导入android.app.Activity;
导入android.app.ListActivity;
导入android.content.Context;
导入android.content.Intent;
导入android.view.Menu;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
公共类MenuActivity扩展了ListActivity{
字符串[]类={“起始点”、“示例1”、“示例2”、“示例3”,
“示例4”、“示例5”、“示例6”};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setListAdapter(新阵列适配器)(MenuActivity.this,
android.R.layout.simple_list_item_1,class));
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
super.onListItemClick(左、右、位置、id);
字符串=类[位置];
试一试{
Class=Class.forName(奶酪);
Intent-ourIntent=新的Intent(MenuActivity.this,ourClass);
星触觉(ourIntent);
}catch(classnotfounde异常){
e、 printStackTrace();
}
}
}

您需要提及完全限定名

Class ourclass = Class.forName("com.example.music."+cheese);
还要确保packagename与清单文件中提到的名称匹配。还要确保声明清单中的所有活动

public static Class<?> forName (String className)

Added in API level 1
Returns a Class object which represents the class with the given name. The name should be the name of a non-primitive class, as described in the class definition. Primitive types can not be found using this method; use int.class or Integer.TYPE instead.

If the class has not yet been loaded, it is loaded and initialized first. This is done through either the class loader of the calling class or one of its parent class loaders. It is possible that a static initializer is run as a result of this call.

Throws
ClassNotFoundException  if the requested class can not be found.
LinkageError    if an error occurs during linkage
ExceptionInInitializerError if an exception occurs during static initialization of a class.
公共静态类forName(字符串className)
在API级别1中添加
返回表示具有给定名称的类的类对象。该名称应为非基本类的名称,如类定义中所述。使用此方法找不到基本类型;请改用int.Class或Integer.TYPE。
如果尚未加载该类,则首先加载并初始化该类。这可以通过调用类的类加载器或其父类加载器之一完成。此调用可能会导致运行静态初始值设定项。
投掷
如果找不到请求的类,则ClassNotFoundException。
链接错误如果链接过程中发生错误
ExceptionInInitializerError如果在类的静态初始化期间发生异常。
当我将意图行更改为“…新意图(MenuActivity.this), “开始点,类)“它仍然不起作用,所以我假设它是 正在引发类.forName()行中已存在的异常,因为 当我提出这个意图时(MenuActivity.this,start point.class)” 在try块之外,它可以工作


您仍然有
classourclass=Class.forName(cheese)
并且如您所见,如果找不到请求的类,它将抛出
ClassNotFoundException
。因为没有提到完全限定名,比如
packaname.classname

当您收到异常或任何错误消息时,请始终发布完整的错误消息。另外,您知道
Class.forName
的作用吗?它期望什么参数?请发布日志