Android 在菜单项选择上启动活动 [英] Android start activity on Menu Item selection
问题描述
我有 2 个班级.一个是基本说明屏幕,在该屏幕上有一个菜单,可让您进入其他课程.另一个类是 MapActivity.我相信问题在于它没有找到其他班级.我尝试了几种不同的方法来声明查找类的意图.这是我尝试过的最新方法:
I have 2 classes. One will be a basic instructions screen and on that screen it will have a menu that will let you go to the other class. The other class is a MapActivity. I believe the problem is that its not finding the other class. I've tried a few different ways of declaring the intent to find the class. This is the latest thing I've tried:
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.goToMap:
Intent intent = new Intent();
intent.setClassName(Main.this, "Map.Class");
startActivity(intent);
return true;
}
return false;
}
它是一个扩展 Activity 的基本类,而 map 类是一个扩展 MapActivity 的基本类(这会导致问题吗?).这是我的清单文件的重要部分:
Its a basic class that extends Activity and the map class is a basic class that extends MapActivity (can that cause a problem?). And here is the important part of my Manifest file:
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Campus_Map"
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=".Main" android:label="Instructions" ></activity>
<activity android:name=".Map" android:label="Map">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
</application>
在查看 LogCat 以找出发生了什么时,我收到了 java.lang.NoClassDefFoundError 和其他一些消息,指出类 ./Map 的链接失败"、无法找到从方法引用的类 ./Map"./Main.run"和VFY:无法解析 const-class 37"
When looking at the LogCat to figure out what was happening, I'm getting a java.lang.NoClassDefFoundError and a few other messages saying "Link of class ./Map failed", "Could Not find class ./Map referenced from method ./Main.run" and "VFY: unable to resolve const-class 37"
推荐答案
你可以这样使用,不要忘记将两个activity都添加到AndroidManifest.xml中:
You can use like this and don't forget to add both activities to AndroidManifest.xml:
Intent launchNewIntent = new Intent(CurrentClass.this,SecondClass.class);
startActivityForResult(launchNewIntent, 0);
这篇关于Android 在菜单项选择上启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!