我是Android App Development的新手。我正在练习 Book Hello, Android中的代码。现在我对菜单感到震惊,无法进一步移动。我想清楚地介绍我的情况,以便有经验的安卓应用程序开发人员指导我。我上传了一张显示状态的图片。当我按下菜单时,我可以找到一个名为设置的选项。
但是按下设置按钮时没有任何反应。我对这个错误感到惊讶,并浪费了大量时间来修复它。但我无法修复它。我想要一些真正的 android 应用程序开发专家来找到我面临的这个问题的解决方案,并帮助我继续前进。
这是我使用的代码。
测试/src/org/anil/test/TestActivity.java
package org.anil.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.content.Intent;
public class TestActivity 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){
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsMenuSelected( MenuItem item){
switch(item.getItemId()){
case R.id.settings:
startActivity(new Intent(this, Prefs.class));
return true;
}
return false;
}
}
测试/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
测试/src/org/anil/test/Prefs.java
package org.anil.test;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Prefs extends PreferenceActivity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
测试/res/menu/menu.xml
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/settings"
android:title="@string/settings_label"
android:alphabeticShortcut="@string/settings_shortcut" />
</menu>
测试/res/xml/settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:key="music"
android:title="@string/music_title"
android:summary="@string/music_summary"
android:defaultValue="true" />
<CheckBoxPreference
android:key="hints"
android:title="@string/hints_title"
android:summary="@string/hints_summary"
android:defaultValue="true"/>
</PreferenceScreen>
测试/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, TestActivity!</string>
<string name="app_name">Test</string>
<string name="settings_label">Settings</string>
<string name="settings_title">Sudoku Settings</string>
<string name="settings_shortcut">s</string>
<string name="music_title">Music</string>
<string name="music_summary">play background music</string>
<string name="hints_title">Hints</string>
<string name="hints_summary">Show hints during play</string>
</resources>
测试/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.anil.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".TestActivity"
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=".Prefs" android:label="@string/settings_title">
</activity>
</application>
</manifest>