gpt4 book ai didi

java - 使用 Class.forName 调用类

转载 作者:行者123 更新时间:2023-12-01 11:26:53 26 4
gpt4 key购买 nike

我正在学习教程,但遇到错误;根据教程,下面的代码必须工作并调用 activity StartingPoint。然而,事实并非如此。我简化了代码,无论如何都只调用 activity,而且它正在工作,所以我只需复制这两个。请您检查一下好吗?

不工作:

public class Menu extends ListActivity{

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

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cheese = classes[position];
try{
Class ourClass = Class.forName("com.thenewboston.travis." + cheese);
Intent ourIntent = new Intent(Menu.this,ourClass);
startActivity(ourIntent);
} catch (ClassNotFoundException e){
e.printStackTrace();
}
}
}

这段代码工作正常,所以项目的其他部分也很好:

public class Menu extends ListActivity{

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

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Intent ourIntent = new Intent("com.thenewboston.travis.STARTINGPOINT");
startActivity(ourIntent);
}
}

我有一个PS。这里也 :)在第二个代码中,我使用大写“STARTINGPOINT”来调用 activity,但是,在原始教程中他使用“StartingPoint”,并将其附加到类的名称中,它是如何工作的?

AndroidManifest.xml

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<activity
android:name=".Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".StartingPoint"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.thenewboston.travis.STARTINGPOINT" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<activity
android:name=".Menu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.thenewboston.travis.MENU" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>

谢谢。 :)

最佳答案

第一个示例使用反射来检索表示您要启动的Activity 的类对象。在第二个 Action 中,您将使用一个 Action 来开始下一个 Activity 。打开您的 AndroidManifest.xml 并查找 com.thenewboston.travis.STARTINGPOINT,您将找到与 Activity 关联的操作

编辑

"example1", "example2"
, "example3", "example4", "example5", "example6"

必须是 Activitiy 子类,并且必须在 AndroidManifest.xml 文件中声明

关于java - 使用 Class.forName 调用类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30751693/

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