致命异常主要 Android 应用程序
我目前正在开发 Android 应用程序。当我单击菜单时,菜单会显示,但如果您选择“首选项”,它就会崩溃。如果您单击“新建”,则效果很好。我不知道发生了什么事。我是 Android 应用程序开发和 eclipse IDE 的新手,所以一切都有帮助。这是 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)
这是崩溃时调用的方法:
/** 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);
}
这是用于此目的的preferences.xml:
<?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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ListPreference
使用String.format()
生成首选项的摘要,因此您可以通过%s
混合来自首选项本身的数据。您似乎使用了%
或描述中令人困惑的String.format()
内容。确保@string/prefs_size_summary
和其他值能够与String.format()
正常运行。另外,通常您不会将字符串资源用于
android:key
,因为您不会翻译您的首选项键。仅将字符串资源用于实际要翻译的内容。ListPreference
usesString.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 confusingString.format()
. Make sure@string/prefs_size_summary
and the others have values that will behave properly withString.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.