Force close message when preferences are called via menu button

Posted by Dan T on Stack Overflow See other posts from Stack Overflow or by Dan T
Published on 2010-06-07T18:29:02Z Indexed on 2010/06/07 18:32 UTC
Read the original article Hit count: 291

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??

© Stack Overflow or respective owner

Related posts about Xml

Related posts about android