通过菜单按钮调用首选项时强制关闭消息
我看代码没有问题。帮助?
Preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:title="Gender"
android:summary="Are you male or female?"
android:key="genderPref"
android:defaultValue="male"
android:entries="@array/genderArray"
android:entryValues="@array/genderValues" />
<ListPreference
android:title="Weight"
android:summary="How much do you weigh?"
android:key="weightPref"
android:defaultValue="180"
android:entries="@array/weightArray"
android:entryValues="@array/weightValues" />
</PreferenceScreen>
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="genderArray">
<item>Male</item>
<item>Female</item>
</string-array>
<string-array name="genderValues">
<item>male</item>
<item>female</item>
</string-array>
<string-array name="weightArray">
<item>120</item>
<item>150</item>
<item>180</item>
<item>210</item>
<item>240</item>
<item>270</item>
</string-array>
<string-array name="weightValues">
<item>120</item>
<item>150</item>
<item>180</item>
<item>210</item>
<item>240</item>
<item>270</item>
</string-array>
</resources>
Preferences.java:butts.xml
package com.dantoth.drinkingbuddy;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Preferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
};
}
(我不知道为什么它是屁股,但我现在已经习惯了。实际上只是设置菜单按钮)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/settings"
android:title="Settings"
android:icon="@drawable/ic_menu_settings" />
<item android:id="@+id/archive"
android:title="Archive"
android:icon="@drawable/ic_menu_archive" />
<item android:id="@+id/new_session"
android:title="New Session"
android:icon="@drawable/ic_menu_new" />
<item android:id="@+id/about"
android:title="About"
android:icon="@drawable/ic_menu_about" />
</menu>
在 DrinkingBuddy.java 中:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
startActivity(new Intent(this, Preferences.class));
return true;
case R.id.archive: Toast.makeText(this, "Expect to see your old drinking sessions here.", Toast.LENGTH_LONG).show();
return true;
//ETC.
} return false;
就是这样。我可以按手机上的菜单按钮并查看我创建的菜单项,但是当我单击“设置”(r.id.settings)时,它会出现FC。我需要对清单/其他事情做任何事情才能使其正常工作吗?
I see no problem in the code. Help?
preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:title="Gender"
android:summary="Are you male or female?"
android:key="genderPref"
android:defaultValue="male"
android:entries="@array/genderArray"
android:entryValues="@array/genderValues" />
<ListPreference
android:title="Weight"
android:summary="How much do you weigh?"
android:key="weightPref"
android:defaultValue="180"
android:entries="@array/weightArray"
android:entryValues="@array/weightValues" />
</PreferenceScreen>
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="genderArray">
<item>Male</item>
<item>Female</item>
</string-array>
<string-array name="genderValues">
<item>male</item>
<item>female</item>
</string-array>
<string-array name="weightArray">
<item>120</item>
<item>150</item>
<item>180</item>
<item>210</item>
<item>240</item>
<item>270</item>
</string-array>
<string-array name="weightValues">
<item>120</item>
<item>150</item>
<item>180</item>
<item>210</item>
<item>240</item>
<item>270</item>
</string-array>
</resources>
Preferences.java:
package com.dantoth.drinkingbuddy;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Preferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
};
}
butts.xml (idk why it's butts but I've gotten used to it now. really just sets up the menu button)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/settings"
android:title="Settings"
android:icon="@drawable/ic_menu_settings" />
<item android:id="@+id/archive"
android:title="Archive"
android:icon="@drawable/ic_menu_archive" />
<item android:id="@+id/new_session"
android:title="New Session"
android:icon="@drawable/ic_menu_new" />
<item android:id="@+id/about"
android:title="About"
android:icon="@drawable/ic_menu_about" />
</menu>
within DrinkingBuddy.java:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
startActivity(new Intent(this, Preferences.class));
return true;
case R.id.archive: Toast.makeText(this, "Expect to see your old drinking sessions here.", Toast.LENGTH_LONG).show();
return true;
//ETC.
} return false;
That's it. I can press the menu button on the phone and see the menu items I created, but when I click on the "Settings" (r.id.settings) it FC. Do I have to do anything to the manifest/other thing to get this to work??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在清单中将
.Preferences
列为
(如果您尚未这样做)。另外,检查 logcat 的输出(从 Eclipse 中的 LogCat 视图,或通过在命令行上运行
adb logcat
)来了解实际问题是什么。You need to have
.Preferences
listed as an<activity>
in your manifest, if you don't already.Also, check the output of logcat (either from the LogCat view in Eclipse, or by running
adb logcat
on the command line) to get an idea of what the actual problem is.