Pergunta

Given my res/xml/preferences.xml looks like this:

<?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>

And I activate these preferences in a SettingsActivity class like this:

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 looks like this:

<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>

And when I'm using that SettingsActivity, the app will crash all the time when opening the page. The stack trace looks like this:

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

No further stacktrace or any other useful log information. I've been trying to remove the preference on start (works, but no use), change its type (not supported) change the key & string types, remove attributes but the app still crashes.

A few lines above I'm using another ListPreference which works well, just this one creates trouble. Anything I missed here?

Foi útil?

Solução

Thanks to @shoerat pointing out that there is no appearant problem in the code above plus the lack of a stack trace I wondered whether this was some internal exception which got caught but wasn't sent "up". A good way to debug this one was (in Android Studio):

  • Open Run > View Breakpoints...
  • Enable the Exception Breakpoints > Any exceptionbreak point configuration
  • Find that java.util.Formatter.checkFlags throws a FormatFlagsConversionMismatchException exception which is not visible ANYWHERE
  • Trace it up to ListPreference.getSummary:

    @Override
    public CharSequence getSummary() {
        final CharSequence entry = getEntry();
        if (mSummary == null || entry == null) {
            return super.getSummary();
        } else {
            return String.format(mSummary, entry);
        }
    }
    
  • Check the summary format string and find that it has illegal contents (a single % instead of %%)

  • Change the format string to contain %% or %s (to include the current value).
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top