Java I';我在Android中使用类对象启动活动时遇到问题

Java I';我在Android中使用类对象启动活动时遇到问题,java,android,Java,Android,因此,我正在关注Newboston的Android教程。我了解如何使用意图开始一项活动。但是,在使用class对象创建新活动时,class.forname()方法内部无法识别类名。但假设我直接使用Intent创建,它在查找类时没有问题。但是使用这个,使用相同的类路径,我得到了java.lang.ClassNotFoundException` 包名称-com.hello.myproject import android.app.ListActivity; import android.co

因此,我正在关注Newboston的Android教程。我了解如何使用意图开始一项活动。但是,在使用class对象创建新活动时,class.forname()方法内部无法识别类名。但假设我直接使用Intent创建,它在查找类时没有问题。但是使用这个,使用相同的类路径,我得到了java.lang.ClassNotFoundException` 包名称-com.hello.myproject

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


public class Menu extends ListActivity{
    String []classes={"MainActivity","example1","example2","example3","example4","example5","example6"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(Menu.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);

        Class myclass=Class.forName("com.hello.myproject.MainActivity");//This class name is not being found, but if I copy the same path inside Intent() it has no problem and yes there is a class called MainActivity
        Intent myintent=new Intent(Menu.this,myclass );
        startActivity(myintent);
    }
}
导入android.app.ListActivity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
公共类菜单扩展了ListActivity{
字符串[]类={“MainActivity”、“example1”、“example2”、“example3”、“example4”、“example5”、“example6”};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setListAdapter(新的ArrayAdapter(Menu.this,android.R.layout.simple_list_item_1,classes));
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
super.onListItemClick(左、右、位置、id);
Class myclass=Class.forName(“com.hello.myproject.MainActivity”);//找不到此类名,但如果我在Intent()中复制相同的路径,则不会有问题,而且有一个名为MainActivity的类
Intent myintent=newintent(Menu.this,myclass);
星触觉(myintent);
}
}
清单文件

<?xml version="1.0" encoding="utf-8"?>



您必须将
.class
的名称传递到您想去的地方。这样使用:

Intent myintent=new Intent(Menu.this, MainActivity.class);
Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent);
用这个

Intent myintent=new Intent(Menu.this, MainActivity.class);

在清单xml文件中定义“MainActivity”和如下调用:

Intent myintent=new Intent(Menu.this, MainActivity.class);
Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent);
希望它对你有用。

Narayan Payyoor

只有在manifest.xml文件中的
Activity
内添加了
intent filter
标记时,用于启动活动的语法才有效

例如:

如果你正在做

Class myclass = Class.forName("com.hello.myproject.MAINACTIVITY");
Intent myintent = new Intent(Menu.this,myclass );
startActivity(myintent);
您必须在清单文件中使用
intent过滤器声明此活动

<activity
    android:name="com.hello.myproject.MainActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait" >
    <intent-filter>
        <action android:name="com.hello.myproject.MAINACTIVITY" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
更新: 由于您想在列表项上打开不同的活动,请单击此处。。我不确定,但它应该有效

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

    Class myclass = Class.forName("com.hello.myproject." + classes[position]);
    Intent myintent = new Intent(Menu.this,myclass );
    startActivity(myintent);
}

快乐编码…

为什么不直接执行Intent myintent=newintent(Menu.this,MainActivity.class)?这样我就不能基于类变量设置Intent了?@narayanpayoor看到我现在发布的答案了吗?谢谢,当我把上面的代码放在try块中时,它工作了。我刚刚学会了使用上面列出的字符串中的值创建listactivity。但是您看到的第一个值MainActivity是一个现有的类,但是当我在列表中单击它时,什么也没有发生。我将位置的所有值存储在一个字符串中。单击列表时,如何移动到字符串中列出的另一个类?希望你理解这个问题嗨,我已经这样做了,但是当我点击一个有类名的项目时什么都没有发生,即当我点击MainActivity时什么都没有发生。我已经将它们包含在清单文件中,类名是sameok now change
String[]classes={“MainActivity”、“example1”、“example2”、“example3”、“example4”、“example5”、“example6”}带有
String[]类={“MAINACTIVITY”、“MENU”、“TEXTPLAY”、“example3”、“example4”、“example5”、“example6”}@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    Class myclass = Class.forName("com.hello.myproject." + classes[position]);
    Intent myintent = new Intent(Menu.this,myclass );
    startActivity(myintent);
}