Preferences not working

Put your problem here if it does not fit any of the other categories.

Preferences not working

Postby Technocrat » Fri Apr 17, 2009 11:02 pm

I keep getting an app stop on my perferences in my project. So I took the AdvancedPrefereneces tutorial from the SDK and I am trying to run it and I am getting the same problem and it's driving me crazy. So I need some help. This is the tutorial. When I click the pref button it dies. Any ideas why it wont work?

After debugging I have found it crashes on:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. startActivityForResult(launchPreferencesIntent, REQUEST_CODE_PREFERENCES);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


AdvancedPrefereneces.java:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.test;
  2.  
  3. import android.content.SharedPreferences;
  4.  
  5. import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
  6.  
  7. import android.os.Bundle;
  8.  
  9. import android.os.Handler;
  10.  
  11. import android.preference.PreferenceActivity;
  12.  
  13. import android.preference.CheckBoxPreference;
  14.  
  15. import android.widget.Toast;
  16.  
  17.  
  18.  
  19. public class AdvancedPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {
  20.  
  21.     public static final String KEY_MY_PREFERENCE = "my_preference";
  22.  
  23.     public static final String KEY_ADVANCED_CHECKBOX_PREFERENCE = "advanced_checkbox_preference";
  24.  
  25.  
  26.  
  27.     private CheckBoxPreference mCheckBoxPreference;
  28.  
  29.     private Handler mHandler = new Handler();
  30.  
  31.  
  32.  
  33.     private Runnable mForceCheckBoxRunnable = new Runnable() {
  34.  
  35.         public void run() {
  36.  
  37.             if (mCheckBoxPreference != null) {
  38.  
  39.                 mCheckBoxPreference.setChecked(!mCheckBoxPreference.isChecked());
  40.  
  41.             }
  42.  
  43.  
  44.  
  45.             // Force toggle again in a second
  46.  
  47.             mHandler.postDelayed(this, 1000);
  48.  
  49.         }
  50.  
  51.     };
  52.  
  53.  
  54.  
  55.     @Override
  56.  
  57.     protected void onCreate(Bundle savedInstanceState) {
  58.  
  59.         super.onCreate(savedInstanceState);
  60.  
  61.  
  62.  
  63.         // Load the XML preferences file
  64.  
  65.         addPreferencesFromResource(R.xml.advanced_preferences);
  66.  
  67.  
  68.  
  69.         // Get a reference to the checkbox preference
  70.  
  71.         mCheckBoxPreference = (CheckBoxPreference)getPreferenceScreen().findPreference(
  72.  
  73.                 KEY_ADVANCED_CHECKBOX_PREFERENCE);
  74.  
  75.     }
  76.  
  77.  
  78.  
  79.     @Override
  80.  
  81.     protected void onResume() {
  82.  
  83.         super.onResume();
  84.  
  85.  
  86.  
  87.         // Start the force toggle
  88.  
  89.         mForceCheckBoxRunnable.run();
  90.  
  91.  
  92.  
  93.         // Set up a listener whenever a key changes
  94.  
  95.         getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
  96.  
  97.     }
  98.  
  99.  
  100.  
  101.     @Override
  102.  
  103.     protected void onPause() {
  104.  
  105.         super.onPause();
  106.  
  107.  
  108.  
  109.         // Unregister the listener whenever a key changes
  110.  
  111.         getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
  112.  
  113.  
  114.  
  115.         mHandler.removeCallbacks(mForceCheckBoxRunnable);
  116.  
  117.     }
  118.  
  119.  
  120.  
  121.     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
  122.  
  123.         // Let's do something when my counter preference value changes
  124.  
  125.         if (key.equals(KEY_MY_PREFERENCE)) {
  126.  
  127.             Toast.makeText(this, "Thanks! You increased my count to "
  128.  
  129.                     + sharedPreferences.getInt(key, 0), Toast.LENGTH_SHORT).show();
  130.  
  131.         }
  132.  
  133.     }
  134.  
  135.  
  136.  
  137. }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


LaunchingPreferences.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.test;
  2.  
  3.  
  4.  
  5. import com.test.R;
  6.  
  7.  
  8.  
  9. import android.app.Activity;
  10.  
  11. import android.content.Intent;
  12.  
  13. import android.content.SharedPreferences;
  14.  
  15. import android.os.Bundle;
  16.  
  17. import android.preference.PreferenceManager;
  18.  
  19. import android.view.View;
  20.  
  21. import android.view.View.OnClickListener;
  22.  
  23. import android.widget.Button;
  24.  
  25. import android.widget.LinearLayout;
  26.  
  27. import android.widget.TextView;
  28.  
  29. import android.widget.Toast;
  30.  
  31. import android.widget.LinearLayout.LayoutParams;
  32.  
  33.  
  34.  
  35. public class LaunchingPreferences extends Activity implements OnClickListener {
  36.  
  37.  
  38.  
  39.     private static final int REQUEST_CODE_PREFERENCES = 1;
  40.  
  41.  
  42.  
  43.     private TextView mCounterText;
  44.  
  45.  
  46.  
  47.     @Override
  48.  
  49.     protected void onCreate(Bundle savedInstanceState) {
  50.  
  51.         super.onCreate(savedInstanceState);
  52.  
  53.  
  54.  
  55.         PreferenceManager.setDefaultValues(this, R.xml.advanced_preferences, false);
  56.  
  57.  
  58.  
  59.         // Simple layout
  60.  
  61.         LinearLayout layout = new LinearLayout(this);
  62.  
  63.         layout.setOrientation(LinearLayout.VERTICAL);
  64.  
  65.         setContentView(layout);
  66.  
  67.  
  68.  
  69.         // Create a simple button that will launch the preferences
  70.  
  71.         Button launchPreferences = new Button(this);
  72.  
  73.         launchPreferences.setText("Pref");
  74.  
  75.         launchPreferences.setOnClickListener(this);
  76.  
  77.         layout.addView(launchPreferences, new LayoutParams(LayoutParams.FILL_PARENT,
  78.  
  79.                 LayoutParams.WRAP_CONTENT));
  80.  
  81.  
  82.  
  83.         mCounterText = new TextView(this);
  84.  
  85.         layout.addView(mCounterText, new LayoutParams(LayoutParams.FILL_PARENT,
  86.  
  87.                 LayoutParams.WRAP_CONTENT));
  88.  
  89.  
  90.  
  91.         updateCounterText();
  92.  
  93.     }
  94.  
  95.  
  96.  
  97.     public void onClick(View v) {
  98.  
  99.  
  100.  
  101.         // When the button is clicked, launch an activity through this intent
  102.  
  103.         Intent launchPreferencesIntent = new Intent().setClass(this, AdvancedPreferences.class);
  104.  
  105.  
  106.  
  107.         // Make it a subactivity so we know when it returns
  108.  
  109.         startActivityForResult(launchPreferencesIntent, REQUEST_CODE_PREFERENCES);
  110.  
  111.     }
  112.  
  113.  
  114.  
  115.     @Override
  116.  
  117.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  118.  
  119.         super.onActivityResult(requestCode, resultCode, data);
  120.  
  121.  
  122.  
  123.         // The preferences returned if the request code is what we had given
  124.  
  125.         // earlier in startSubActivity
  126.  
  127.         if (requestCode == REQUEST_CODE_PREFERENCES) {
  128.  
  129.             // Read a sample value they have set
  130.  
  131.             updateCounterText();
  132.  
  133.         }
  134.  
  135.     }
  136.  
  137.  
  138.  
  139.     private void updateCounterText() {
  140.  
  141.         // Since we're in the same package, we can use this context to get
  142.  
  143.         // the default shared preferences
  144.  
  145.         SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
  146.  
  147.         final int counter = sharedPref.getInt(AdvancedPreferences.KEY_MY_PREFERENCE, 0);
  148.  
  149.         mCounterText.setText("123 " + counter);
  150.  
  151.     }
  152.  
  153. }
Parsed in 0.045 seconds, using GeSHi 1.0.8.4


advanced_preferences.xml

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <PreferenceScreen
  4.  
  5.        xmlns:android="http://schemas.android.com/apk/res/android">
  6.  
  7.  
  8.  
  9.     <CheckBoxPreference
  10.  
  11.            android:key="advanced_checkbox_preference"
  12.  
  13.            android:title="test" />
  14.  
  15.  
  16.  
  17. </PreferenceScreen>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4
User avatar
Technocrat
Freshman
Freshman
 
Posts: 2
Joined: Fri Apr 17, 2009 10:51 pm

Top

Postby Technocrat » Fri Apr 17, 2009 11:45 pm

I figured it out. Turns out someone else had the same issue on the Google Mailing list. The issue was I did not have the class defined in my manifest :oops:
User avatar
Technocrat
Freshman
Freshman
 
Posts: 2
Joined: Fri Apr 17, 2009 10:51 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot], Yahoo [Bot] and 20 guests