问题
I am currently working on an Android app. When I click Menu, the Menu shows up, but then if you choose "Preferences" it crashes. If you click "New" it works fine. I don't know what is going on. I am new to Android App development and the eclipse IDE, so anything helps. Here is the LogCat:
11-14 12:14:44.665: E/AndroidRuntime(392): FATAL EXCEPTION: main
11-14 12:14:44.665: E/AndroidRuntime(392): java.util.UnknownFormatConversionException: Conversion: r
11-14 12:14:44.665: E/AndroidRuntime(392): at java.util.Formatter$FormatToken.unknownFormatConversionException(Formatter.java:1420)
11-14 12:14:44.665: E/AndroidRuntime(392): at java.util.Formatter$FormatToken.checkFlags(Formatter.java:1357)
11-14 12:14:44.665: E/AndroidRuntime(392): at java.util.Formatter.transform(Formatter.java:1463)
11-14 12:14:44.665: E/AndroidRuntime(392): at java.util.Formatter.doFormat(Formatter.java:1102)
11-14 12:14:44.665: E/AndroidRuntime(392): at java.util.Formatter.format(Formatter.java:1063)
11-14 12:14:44.665: E/AndroidRuntime(392): at java.util.Formatter.format(Formatter.java:1032)
11-14 12:14:44.665: E/AndroidRuntime(392): at java.lang.String.format(String.java:2104)
11-14 12:14:44.665: E/AndroidRuntime(392): at java.lang.String.format(String.java:2078)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.preference.ListPreference.getSummary(ListPreference.java:152)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.preference.Preference.onBindView(Preference.java:500)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.preference.Preference.getView(Preference.java:450)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.preference.PreferenceGroupAdapter.getView(PreferenceGroupAdapter.java:221)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.widget.AbsListView.obtainView(AbsListView.java:1949)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.widget.ListView.measureHeightOfChildren(ListView.java:1228)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.widget.ListView.onMeasure(ListView.java:1139)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.view.View.measure(View.java:10828)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1284)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.widget.LinearLayout.measureVertical(LinearLayout.java:613)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.view.View.measure(View.java:10828)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1284)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:956)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.widget.LinearLayout.onMeasure(LinearLayout.java:521)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.view.View.measure(View.java:10828)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1284)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.widget.LinearLayout.measureVertical(LinearLayout.java:613)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.view.View.measure(View.java:10828)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.view.View.measure(View.java:10828)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.view.View.measure(View.java:10828)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1284)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.widget.LinearLayout.measureVertical(LinearLayout.java:613)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.view.View.measure(View.java:10828)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
11-14 12:14:44.665: E/AndroidRuntime(392): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:1890)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.view.View.measure(View.java:10828)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.view.ViewRoot.performTraversals(ViewRoot.java:884)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.view.ViewRoot.handleMessage(ViewRoot.java:2003)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.os.Looper.loop(Looper.java:132)
11-14 12:14:44.665: E/AndroidRuntime(392): at android.app.ActivityThread.main(ActivityThread.java:4025)
11-14 12:14:44.665: E/AndroidRuntime(392): at java.lang.reflect.Method.invokeNative(Native Method)
11-14 12:14:44.665: E/AndroidRuntime(392): at java.lang.reflect.Method.invoke(Method.java:491)
11-14 12:14:44.665: E/AndroidRuntime(392): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
11-14 12:14:44.665: E/AndroidRuntime(392): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
11-14 12:14:44.665: E/AndroidRuntime(392): at dalvik.system.NativeStart.main(Native Method)
Here is the method that is called at the time of the crash:
/** when menu button option selected */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_options:
startActivity(new Intent(this, WordSearchPreferences.class));
return true;
case R.id.menu_new:
control.newWordSearch();
return true;
}return super.onOptionsItemSelected(item);
}
This is the preferences.xml that is used for this:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference android:key="@string/prefs_size"
android:title="@string/size" android:defaultValue="10"
android:summary="@string/prefs_size_summary"
android:entryValues="@array/sizes_list_values"
android:entries="@array/sizes_list_labels" />
<ListPreference android:key="@string/prefs_category"
android:title="@string/category" android:defaultValue="@string/RANDOM"
android:summary="@string/prefs_category_summary"
android:entryValues="@array/categories_list_values"
android:entries="@array/categories_list_labels" />
<ListPreference android:key="@string/prefs_touch_mode"
android:title="@string/input_type" android:defaultValue="@string/DRAG"
android:summary="@string/prefs_touch_mode_summary"
android:entryValues="@array/touch_mode_list_values"
android:entries="@array/touch_mode_list_labels" />
<ListPreference android:key="@string/PREFS_THEME"
android:title="@string/THEME" android:defaultValue="@string/THEME_ORIGINAL"
android:summary="@string/PREFS_THEME_SUMMARY"
android:entryValues="@array/THEME_VALUES"
android:entries="@array/THEME_LABELS" />
</PreferenceScreen>
回答1:
ListPreference
uses String.format()
to generate the summary for the preference, so you can blend in data from the preference itself via %s
. You appear to be using %
or something in the description that is confusing String.format()
. Make sure @string/prefs_size_summary
and the others have values that will behave properly with String.format()
.
Also, usually you don't use string resources for android:key
, because you are not going to be translating your preference keys. Only use string resources for things that will actually be translated.
来源:https://stackoverflow.com/questions/8125524/fatal-exception-main-android-app