{"id":1424,"date":"2022-08-30T15:16:25","date_gmt":"2022-08-30T15:16:25","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/16\/using-sharedpreferences-on-a-onclick-method-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:16:25","modified_gmt":"2022-08-30T15:16:25","slug":"using-sharedpreferences-on-a-onclick-method-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/using-sharedpreferences-on-a-onclick-method-collection-of-common-programming-errors\/","title":{"rendered":"Using SharedPreferences on a onClick method-Collection of common programming errors"},"content":{"rendered":"<p>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.<\/p>\n<p>array.xml is like this:<\/p>\n<pre><code>\n    \n        1\n        2\n        3\n    \n\n    \n        @string\/site1\n        @string\/site2\n        @string\/site3\n    \n\n<\/code><\/pre>\n<p>preference.xml:<\/p>\n<pre><code>\n    \n\n<\/code><\/pre>\n<p>This is PreferenceActivity:<\/p>\n<pre><code>public void public class MyPreference extends PreferenceActivity {\n    public static final int DEFAULT_SITE = 1;\n    public static final String FAV_SITE = \"websites\";\n\n    @SuppressWarnings(\"deprecation\")\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        addPreferencesFromResource(R.xml.preferences);\n    }\n\n}\n<\/code><\/pre>\n<p>And finally this is the onClick method of MainActivity that doesn&#8217;t work, with a &#8220;The application stopped unexpectedly&#8221; error:<\/p>\n<pre><code>public void click(View v) {\n    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);\n    String url;\n    switch (pref.getInt(MyPreference.FAV_SITE,\n                        MyPreference.DEFAULT_SITE)) {\n        case 1:\n        default:\n            url = \"http:\/\/www.google.com\";\n            break;\n        case 2:\n            url = \"http:\/\/www.youtube.com\";\n            break;\n        case 3:\n            url = \"http:\/\/www.facebook.com\";\n            break;\n     }\n     Intent BrowserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));\n     startActivity(BrowserIntent);\n}\n<\/code><\/pre>\n<p>What am I doing wrong? onResume already uses SharedPreferences and works perfectly.<\/p>\n<p>Edit:<\/p>\n<p>This is the LogCat<\/p>\n<pre><code>07-08 16:18:27.540: W\/dalvikvm(3041): threadid=1: thread exiting with uncaught exception (group=0xb67f44f0)\n07-08 16:18:27.570: E\/AndroidRuntime(3041): FATAL EXCEPTION: main\n07-08 16:18:27.570: E\/AndroidRuntime(3041): java.lang.IllegalStateException: Could not execute method of the activity\n07-08 16:18:27.570: E\/AndroidRuntime(3041):     at android.view.View$1.onClick(View.java:2144)\n07-08 16:18:27.570: E\/AndroidRuntime(3041):     at android.view.View.performClick(View.java:2485)\n07-08 16:18:27.570: E\/AndroidRuntime(3041):     at android.view.View$PerformClick.run(View.java:9080)\n07-08 16:18:27.570: E\/AndroidRuntime(3041):     at android.os.Handler.handleCallback(Handler.java:587)\n07-08 16:18:27.570: E\/AndroidRuntime(3041):     at android.os.Handler.dispatchMessage(Handler.java:92)\n07-08 16:18:27.570: E\/AndroidRuntime(3041):     at android.os.Looper.loop(Looper.java:130)\n07-08 16:18:27.570: E\/AndroidRuntime(3041):     at android.app.ActivityThread.main(ActivityThread.java:3683)\n07-08 16:18:27.570: E\/AndroidRuntime(3041):     at java.lang.reflect.Method.invokeNative(Native Method)\n07-08 16:18:27.570: E\/AndroidRuntime(3041):     at java.lang.reflect.Method.invoke(Method.java:507)\n07-08 16:18:27.570: E\/AndroidRuntime(3041):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)\n07-08 16:18:27.570: E\/AndroidRuntime(3041):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)\n07-08 16:18:27.570: E\/AndroidRuntime(3041):     at dalvik.system.NativeStart.main(Native Method)\n07-08 16:18:27.570: E\/AndroidRuntime(3041): Caused by: java.lang.reflect.InvocationTargetException\n07-08 16:18:27.570: E\/AndroidRuntime(3041):     at java.lang.reflect.Method.invokeNative(Native Method)\n07-08 16:18:27.570: E\/AndroidRuntime(3041):     at java.lang.reflect.Method.invoke(Method.java:507)\n07-08 16:18:27.570: E\/AndroidRuntime(3041):     at android.view.View$1.onClick(View.java:2139)\n07-08 16:18:27.570: E\/AndroidRuntime(3041):     ... 11 more\n07-08 16:18:27.570: E\/AndroidRuntime(3041): Caused by: java.lang.ClassCastException: java.lang.String\n07-08 16:18:27.570: E\/AndroidRuntime(3041):     at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2857)\n07-08 16:18:27.570: E\/AndroidRuntime(3041):     at it.megaforum.megaapp.MainActivity.clickMegaforum(MainActivity.java:38)\n07-08 16:18:27.570: E\/AndroidRuntime(3041):     ... 14 more\n<\/code><\/pre>\n<p>Edit 2:<\/p>\n<p>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.<\/p>\n<ol>\n<li>\n<p>I would guess the &#8220;this&#8221; in:<\/p>\n<pre><code>PreferenceManager.getDefaultSharedPreferences(this);\n<\/code><\/pre>\n<p>doesn&#8217;t fit as you are most likely in an inline class (the listener for the button), which means &#8220;this&#8221; references the listener class instead of the outer class (your activity).<\/p>\n<p>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.<\/p>\n<\/li>\n<li>\n<p>The problem was on the integer-array. I changed it to a string-array and now it works. This is the code.<\/p>\n<p>array.xml<\/p>\n<pre><code>\n    \n        1\n        2\n        3\n    \n\n    \n        @string\/site1\n        @string\/site2\n        @string\/site3\n    \n\n<\/code><\/pre>\n<p>MyPreference<\/p>\n<pre><code>public void public class MyPreference extends PreferenceActivity {\n    public static final String DEFAULT_SITE = \"1\";\n    public static final String FAV_SITE = \"websites\";\n\n    @SuppressWarnings(\"deprecation\")\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        addPreferencesFromResource(R.xml.preferences);\n    }\n}\n<\/code><\/pre>\n<p>Listener<\/p>\n<pre><code>public void click(View v) {\n    SharedPreferences pref = PreferenceManager\n            .getDefaultSharedPreferences(MainActivity.this);\n    String url = \"http:\/\/\";\n    if (pref.getString(MyPreference.FAV_SITE, MyPreference.DEFAULT_SITE).equals(\"1\") {\n        url = \"http:\/\/www.google.com\";\n    } else if (pref.getString(MyPreference.FAV_SITE, MyPreference.DEFAULT_SITE).equals(\"2\")) {\n        url = \"http:\/\/www.youtube.com\";\n    } else if (pref.getString(MyPreference.FAV_SITE, MyPreference.DEFAULT_SITE).equals(\"3\")) {\n        url = \"http:\/\/www.facebook.com\";\n    }\n    Intent BrowserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));\n    startActivity(BrowserIntent);\n<\/code><\/pre>\n<p>}<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-16 20:50:17. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1424","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1424","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1424"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1424\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}