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

发布于 2024-09-04 09:31:08 字数 3132 浏览 3 评论 0原文

我看代码没有问题。帮助?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弃爱 2024-09-11 09:31:08

您需要在清单中将 .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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文