【发布时间】:2017-04-06 19:46:28
【问题描述】:
我正在努力让活动从我的 android 应用程序中的选项菜单开始。
有问题的选项用于帮助页面。我用于此的代码(开关盒)如下,帮助选项位于底部:
@Override
// Respond to item selected on OPTIONS MENU
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
//put data in Intent
case R.id.settings:
Toast.makeText(this, "Settings chosen", Toast.LENGTH_SHORT).show();
return true;
case R.id.easy:
Toast.makeText(this, "Easy chosen", Toast.LENGTH_SHORT).show();
return true;
case R.id.medium:
Toast.makeText(this, "Medium chosen", Toast.LENGTH_SHORT).show();
return true;
case R.id.hard:
Toast.makeText(this, "Hard chosen", Toast.LENGTH_SHORT).show();
return true;
case R.id.scores:
Toast.makeText(this, "High Scores chosen", Toast.LENGTH_SHORT).show();
return true;
case R.id.help:
Intent intent = new Intent(this, HelpActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
我认为我在清单中正确设置了它,但是当我单击帮助选项时,应用程序就崩溃了。我的清单如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cct.mad.lab"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="cct.mad.lab.MainMenu"
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=".GameActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name=".HelpActivity"
android:label="@string/app_name" >
</activity>
我会假设我做错了一些对其他人来说很明显但我现在看不到的错误。
感谢您的帮助
编辑:我正在添加我的 HelpActivity 类,因为我认为它可能不正确。我是一个相对新手:
public class HelpActivity extends Activity {
public void init() {
Intent helpScreen = new Intent(HelpActivity.this, MainMenu.class);
startActivity(helpScreen);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help_page);
init();
}
InputStream iFile = getResources().openRawResource(R.raw.gamehelp);
private String inputStreamToString(InputStream iFile) {
TextView helpText = (TextView) findViewById(R.id.tvHelpText);
String strFile = inputStreamToString(iFile);
helpText.setText(strFile);
return strFile;
}
}
【问题讨论】:
-
它在 logcat 中给出了什么错误?
-
据我所知,它没有给出任何结果
-
你说当你点击按钮时,应用程序就崩溃了,那它怎么不会给你任何错误?
-
在 logcat 中看,这次什么都没有
-
那么您的 HelpActivity java 文件或其布局文件可能有问题?
标签: java android android-activity switch-statement android-manifest