通过菜单按钮调用首选项时强制关闭消息

时间:2010-06-07 18:29:02

标签: xml android menu preferences android-preferences

我发现代码中没有问题。帮助

的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为什么它是屁股,但我现在已经习惯了。真的只是设置菜单按钮)

<?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>
在DrinkBuddy.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。我是否必须对清单/其他事情做任何事情才能让它发挥作用?

1 个答案:

答案 0 :(得分:1)

如果您尚未在清单中列出.Preferences,则需要<activity>

另外,检查logcat的输出(从Eclipse中的LogCat视图,或在命令行上运行adb logcat),以了解实际问题是什么。