滚动时ListPreference崩溃

时间:2014-01-20 16:04:54

标签: android android-preferences

鉴于我的res/xml/preferences.xml看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orderingFromXml="true">
    ... 
    <PreferenceCategory android:title="@string/reminders">
        <ListPreference
                android:key="PREF_BATTERY_LOW"
                android:title="@string/battery_low"
                android:summary="@string/battery_low_desc"
                android:entries="@array/battery_strings"
                android:entryValues="@array/battery_values"
                android:dialogTitle="@string/battery_low"
                android:defaultValue="25"
                />
    </PreferenceCategory>
    ...
</PreferenceScreen>

我在这样的SettingsActivity类中激活这些首选项:

import android.preference.PreferenceActivity;

public class SettingsActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.preferences);
    }
}

src / main / res / values / array.xml如下所示:

<string-array name="battery_values">
    <item>5</item>
    <item>10</item>
    <item>15</item>
    <item>20</item>
    <item>25</item>
    <item>30</item>
    <item>40</item>
    <item>50</item>
</string-array>

<string-array name="battery_strings">
    <item>5 %</item>
    <item>10 %</item>
    <item>15 %</item>
    <item>20 %</item>
    <item>25 %</item>
    <item>30 %</item>
    <item>40 %</item>
    <item>50 %</item>
</string-array>

当我使用那个SettingsActivity时,应用程序会在打开页面时一直崩溃。堆栈跟踪如下所示:

E/InputEventReceiver(28227): Exception dispatching input event.
D/AndroidRuntime(28227): Shutting down VM
W/dalvikvm(28227): threadid=1: thread exiting with uncaught exception (group=0xa613b908)
I/AndroidRuntime(28227): VM exiting with result code 99, cleanup skipped.
I/ActivityManager(  432): Process com.company.app (pid 28227) has died.
D/dalvikvm(28249): Late-enabling CheckJNI
W/ActivityManager(  432): Scheduling restart of crashed service com.company.app/.services.LocationService in 5000ms
W/ActivityManager(  432): Force removing ActivityRecord{a6bfe408 u0 com.company.app/.SettingsActivity}: app died, no saved state
I/WindowState(  432): WIN DEATH: Window{a6cb7078 u0 com.company.app/com.company.app.main.MainActivity}
I/WindowState(  432): WIN DEATH: Window{a6cfea00 u0 com.company.app/com.company.app.SettingsActivity}
I/ActivityManager(  432): Start proc com.company.app for activity com.company.app/.main.MainActivity: pid=28249 uid=10064 gids={50064, 3003, 1028}
D/Zygote  (  160): Process 28227 exited cleanly (99)
E/Trace   (28249): error opening trace file: No such file or directory (2)
E/jdwp    (28249): Failed sending reply to debugger: Broken pipe
D/dalvikvm(28249): Debugger has detached; object registry had 1 entries
D/dalvikvm(28249): GC_FOR_ALLOC freed 126K, 2% free 8781K/8960K, paused 3ms, total 3ms
D/libEGL  (28249): loaded /system/lib/egl/libEGL_emulation.so
D/        (28249): HostConnection::get() New Host Connection established 0xb78b4fd8, tid 28249
D/libEGL  (28249): loaded /system/lib/egl/libGLESv1_CM_emulation.so
D/libEGL  (28249): loaded /system/lib/egl/libGLESv2_emulation.so
W/EGL_emulation(28249): eglSurfaceAttrib not implemented
D/OpenGLRenderer(28249): Enabling debug mode 0

没有进一步的堆栈跟踪或任何其他有用的日志信息。 我一直试图删除开始时的偏好(工作,但没有用),更改其类型(不支持)更改密钥&amp;字符串类型,删除属性,但应用程序仍然崩溃。

上面几行我正在使用另一个运行良好的ListPreference,只是这个会造成麻烦。我在这里错过了什么?

1 个答案:

答案 0 :(得分:1)

感谢@shoerat指出上面的代码中没有外观问题加上缺少堆栈跟踪,我想知道这是否是一些内部异常被捕获但未被发送“up”。调试这个的好方法是(在Android Studio中):

  • Open Run&gt;查看断点...
  • 启用例外断点&gt;任何异常break point configuration
  • 发现java.util.Formatter.checkFlags抛出了一个不可见的FormatFlagsConversionMismatchException异常ANYWHERE
  • 将其追踪到ListPreference.getSummary:

    @Override
    public CharSequence getSummary() {
        final CharSequence entry = getEntry();
        if (mSummary == null || entry == null) {
            return super.getSummary();
        } else {
            return String.format(mSummary, entry);
        }
    }
    
  • 检查摘要格式字符串,发现它有非法内容(单个%而不是%%)

  • 将格式字符串更改为包含%%或%s(以包含当前值)。