如果数组中有空格(例如“Hello World”),如何打开新意图我正在尝试访问的类名为startingPoint。
它给了我一个错误,因为 - >中没有任何空格。机器人:名称= “”
有解决方法吗?
提前致谢
Root.java
public class Root extends ListActivity {
String classes[] = { "Hello World", "Another Item", "Email"};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Root.this,
R.layout.root, 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 MENU_CHOICE = classes[position];
try {
Class ourClass = Class.forName("se.hello.visboken." + MENU_CHOICE);
Intent ourIntent = new Intent(Root.this, ourClass);
startActivity(ourIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
我的清单看起来像这样
<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=".Root"
android:label="@string/app_name">
<intent-filter>
<action android:name="se.hello.visboken.ROOT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".startingPoint"
android:label="Hello World" >
<intent-filter>
<action android:name="se.hello.visboken.startingPoint" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
答案 0 :(得分:0)
在您的情况下,您需要启动的活动是startingPoint
,不是 Hello World
。 Hello World 是该活动的标题。
<强>更新强>
创建另一个相同大小(必须)的字符串数组,但其中包含类的名称,而不是标题。
String tmp[] = { "startingPoint", "anotherClass1", "anotherClass2"};
并在 onListItemClick 中,按以下方式阅读:
String MENU_CHOICE = tmp[position];