从Android的选项菜单启动活动 [英] Launching activity from android options menu
问题描述
我已经经历了这些论坛四下寻找解决这一问题,即使有似乎是解决方案,他们都不是为我工作。所以这里去。
我是一个新手,Android的发展。我有一个选项菜单的应用程序。当我点击一个选项,我希望它推出一个新的活动 - 但我不断收到错误
意图不能被解析为一个类型
在home.java就行了:
意向意图=新的意图(这一点,about.class);
下面是我所有的code,我相信是相关的。请让我知道如果你需要看到的任何东西。正如我所说,我试图跟随其他问题,但没有人似乎为我工作(AS-在下面的code似乎工作的其他人)。任何帮助将是真棒。
我有我的菜单在RES /菜单/ main_menu.xml定义为:
< XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目机器人:ID =@ + ID /家
机器人:图标=@可绘制/ ic_menu_home
机器人:标题=@字符串/家/>
<项目机器人:ID =@ + ID /约
机器人:图标=@可绘制/ ic_menu_about
机器人:标题=@字符串/关于/>
< /菜单>
我有两个活动 - home.java和about.java。 Home.java是活动的启动,当应用程序被启动,如下图所示。
包ca.example.home;
进口android.app.Activity;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.MenuInflater;
进口android.view.MenuItem;
公共类家庭延伸活动{
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
}
公共布尔onCreateOptionsMenu(功能菜单){
MenuInflater充气= getMenuInflater();
inflater.inflate(R.menu.main_menu,菜单);
返回true;
}
公共布尔onOptionsItemSelected(菜单项项){
//处理项目选择
开关(item.getItemId()){
案例R.id.home:
返回true;
案例R.id.about:
意向意图=新的意图(这一点,about.class);
startActivity(意向);
返回true;
默认:
返回super.onOptionsItemSelected(项目);
}
}
}
About.java要推出新的活动,如下所示:
包ca.brianmccain.nbla;
进口android.app.Activity;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.MenuInflater;
关于公共类扩展活动{
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.about);
}
公共布尔onCreateOptionsMenu(功能菜单){
MenuInflater充气= getMenuInflater();
inflater.inflate(R.menu.main_menu,菜单);
返回true;
}
}
我已经改变了清单为:
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=ca.example.home
安卓版code =1
机器人:VERSIONNAME =1.0>
<使用-SDK安卓的minSdkVersion =8/>
<应用机器人:图标=@可绘制/图标机器人:标签=@字符串/ APP_NAME机器人:可调试=真正的>
<活动机器人:家的名字=
机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动机器人:名称=。关于>
< /活性GT;
< /用途>
< /舱单>
您必须导入意图类。
进口android.content.Intent;
如果你得到一个类似的错误,如果你使用的是Eclipse, preSS按Ctrl-Shift-O组合(组织导入) - 这将搜索所有必要的进口,并将它们添加到文件
I have looked through these forums to find a solution to this problem, and even though there appear to be solutions, none of them seem to be working for me. So here goes.
I am a newbie to Android development. I have an app with an options menu. When I click on one to the options, I want it to launch a new activity - but I keep getting the error
Intent cannot be resolved to a type
in home.java on the line:
Intent intent = new Intent(this, about.class);
Below is all of my code that I believe is relevant. Please let me know if you need to see anything else. As I said, I have tried to follow other questions, but none of them seem to work for me (as-in the below code seems to work for everyone else). Any help would be awesome.
I have my menu defined in res/menu/main_menu.xml by:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/home"
android:icon="@drawable/ic_menu_home"
android:title="@string/home" />
<item android:id="@+id/about"
android:icon="@drawable/ic_menu_about"
android:title="@string/about" />
</menu>
I have two activities - home.java and about.java. Home.java is the activity that is launched when the app is launched and is shown below.
package ca.example.home;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class home extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.home:
return true;
case R.id.about:
Intent intent = new Intent(this, about.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
About.java is the new activity to be launched and is shown below:
package ca.brianmccain.nbla;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
public class about extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
}
I have changed the manifest to:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ca.example.home"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
<activity android:name=".home"
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=".about">
</activity>
</application>
</manifest>
You have to import the intent class.
import android.content.Intent;
If you ever get a similar error, and if you are using eclipse, press Ctrl-Shift-O ("Organize imports") - this searches for all required imports and adds them to the file.
这篇关于从Android的选项菜单启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!