Using SharedPreferences on a onClick method-Collection of common programming errors

I have a button in my activity, and I want to choose in my preference activity what internet website open when I click on it.

array.xml is like this:


    
        1
        2
        3
    

    
        @string/site1
        @string/site2
        @string/site3
    

preference.xml:


    

This is PreferenceActivity:

public void public class MyPreference extends PreferenceActivity {
    public static final int DEFAULT_SITE = 1;
    public static final String FAV_SITE = "websites";

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }

}

And finally this is the onClick method of MainActivity that doesn’t work, with a “The application stopped unexpectedly” error:

public void click(View v) {
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
    String url;
    switch (pref.getInt(MyPreference.FAV_SITE,
                        MyPreference.DEFAULT_SITE)) {
        case 1:
        default:
            url = "http://www.google.com";
            break;
        case 2:
            url = "http://www.youtube.com";
            break;
        case 3:
            url = "http://www.facebook.com";
            break;
     }
     Intent BrowserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
     startActivity(BrowserIntent);
}

What am I doing wrong? onResume already uses SharedPreferences and works perfectly.

Edit:

This is the LogCat

07-08 16:18:27.540: W/dalvikvm(3041): threadid=1: thread exiting with uncaught exception (group=0xb67f44f0)
07-08 16:18:27.570: E/AndroidRuntime(3041): FATAL EXCEPTION: main
07-08 16:18:27.570: E/AndroidRuntime(3041): java.lang.IllegalStateException: Could not execute method of the activity
07-08 16:18:27.570: E/AndroidRuntime(3041):     at android.view.View$1.onClick(View.java:2144)
07-08 16:18:27.570: E/AndroidRuntime(3041):     at android.view.View.performClick(View.java:2485)
07-08 16:18:27.570: E/AndroidRuntime(3041):     at android.view.View$PerformClick.run(View.java:9080)
07-08 16:18:27.570: E/AndroidRuntime(3041):     at android.os.Handler.handleCallback(Handler.java:587)
07-08 16:18:27.570: E/AndroidRuntime(3041):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 16:18:27.570: E/AndroidRuntime(3041):     at android.os.Looper.loop(Looper.java:130)
07-08 16:18:27.570: E/AndroidRuntime(3041):     at android.app.ActivityThread.main(ActivityThread.java:3683)
07-08 16:18:27.570: E/AndroidRuntime(3041):     at java.lang.reflect.Method.invokeNative(Native Method)
07-08 16:18:27.570: E/AndroidRuntime(3041):     at java.lang.reflect.Method.invoke(Method.java:507)
07-08 16:18:27.570: E/AndroidRuntime(3041):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-08 16:18:27.570: E/AndroidRuntime(3041):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-08 16:18:27.570: E/AndroidRuntime(3041):     at dalvik.system.NativeStart.main(Native Method)
07-08 16:18:27.570: E/AndroidRuntime(3041): Caused by: java.lang.reflect.InvocationTargetException
07-08 16:18:27.570: E/AndroidRuntime(3041):     at java.lang.reflect.Method.invokeNative(Native Method)
07-08 16:18:27.570: E/AndroidRuntime(3041):     at java.lang.reflect.Method.invoke(Method.java:507)
07-08 16:18:27.570: E/AndroidRuntime(3041):     at android.view.View$1.onClick(View.java:2139)
07-08 16:18:27.570: E/AndroidRuntime(3041):     ... 11 more
07-08 16:18:27.570: E/AndroidRuntime(3041): Caused by: java.lang.ClassCastException: java.lang.String
07-08 16:18:27.570: E/AndroidRuntime(3041):     at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2857)
07-08 16:18:27.570: E/AndroidRuntime(3041):     at it.megaforum.megaapp.MainActivity.clickMegaforum(MainActivity.java:38)
07-08 16:18:27.570: E/AndroidRuntime(3041):     ... 14 more

Edit 2:

I noticed that another error appears when I click on the preference in MyPreference activity. If I change from integer-array to string-array this error disappears.

  1. I would guess the “this” in:

    PreferenceManager.getDefaultSharedPreferences(this);
    

    doesn’t fit as you are most likely in an inline class (the listener for the button), which means “this” references the listener class instead of the outer class (your activity).

    Edit: To solve this you just need a reference on the preferences in the activity. Set it when the activity is created and then use it in the Listener. Or you just call a method from the activity that handels everything.

  2. The problem was on the integer-array. I changed it to a string-array and now it works. This is the code.

    array.xml

    
        
            1
            2
            3
        
    
        
            @string/site1
            @string/site2
            @string/site3
        
    
    

    MyPreference

    public void public class MyPreference extends PreferenceActivity {
        public static final String DEFAULT_SITE = "1";
        public static final String FAV_SITE = "websites";
    
        @SuppressWarnings("deprecation")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
        }
    }
    

    Listener

    public void click(View v) {
        SharedPreferences pref = PreferenceManager
                .getDefaultSharedPreferences(MainActivity.this);
        String url = "http://";
        if (pref.getString(MyPreference.FAV_SITE, MyPreference.DEFAULT_SITE).equals("1") {
            url = "http://www.google.com";
        } else if (pref.getString(MyPreference.FAV_SITE, MyPreference.DEFAULT_SITE).equals("2")) {
            url = "http://www.youtube.com";
        } else if (pref.getString(MyPreference.FAV_SITE, MyPreference.DEFAULT_SITE).equals("3")) {
            url = "http://www.facebook.com";
        }
        Intent BrowserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(BrowserIntent);
    

    }

Originally posted 2013-11-16 20:50:17.