我的活动中有一个按钮,我想在我的偏好活动中选择单击它时打开的互联网网站。
array.xml 是这样的:
<resources>
<integer-array name="sites_values">
<item>1</item>
<item>2</item>
<item>3</item>
</integer-array>
<string-array name="sites">
<item>@string/site1</item>
<item>@string/site2</item>
<item>@string/site3</item>
</string-array>
</resources>
偏好.xml:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<ListPreference
android:defaultValue="1"
android:entries="@array/sites"
android:entryValues="@array/sites_values"
android:key="websites"
android:persistent="true"
android:summary="@string/summary"
android:title="@string/title" />
</PreferenceScreen>
这是 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);
}
}
最后这是 MainActivity 的 onClick 方法不起作用,出现“应用程序意外停止”错误:
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);
}
我究竟做错了什么?onResume 已经使用 SharedPreferences 并且可以完美运行。
编辑:
这是 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
编辑2:
我注意到当我单击 MyPreference 活动中的首选项时出现另一个错误。如果我从整数数组更改为字符串数组,此错误就会消失。