Android: RuntimeException with PreferenceScreen and Custom Attributes from inside a Library Project
I recently split my project and created a library project and a main project. Having a preferences screen that has custom attributes, i removed the preferences with custom attributes from my preferences.xml, put them into their own xml files, included them back into the preferences.xml file and redefined the individual files in the main project (the process is detailed in the answer to another question here.
The project buil开发者_运维百科d and run properly. However, I am getting a RuntimeException whenever i try to open the preferences screen. Removing the prefs with custom attrs fixes the problem, so i have traced it back to there. Unfortunately there is no useful information in the exception.
attrs.xml (exists in lib project)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="numberpickerPref">
<attr name="maxValue" format="integer" />
<attr name="minValue" format="integer" />
</declare-styleable>
</resources>
preferences.xml (also in lib project)
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="@string/pref_VibrationSettingsTitle">
<CheckBoxPreference android:key="@string/pref_vibrateFlagKey"
android:title="@string/pref_VibrateTitle"
android:summary="@string/pref_VibrateSummary"
android:defaultValue="false" />
<include layout="@layout/pref_vibrate_on" />
<include layout="@layout/pref_vibrate_off" />
</PreferenceCategory>
</PreferenceScreen>
pref_vibrate_off.xml (defined in both lib and main projects) (only diff is the my namespace, one points to the lib, the other the main project)
<?xml version="1.0" encoding="utf-8"?>
<!-- Stupid workaround because Android still has a bug where custom attributes in a library cause the executable project
problems when building:
https://stackoverflow.com/questions/4461407/help-with-a-custom-view-attributes-inside-a-android-library-project -->
<com.me.numberpicker.NumberPickerPreference
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:my="http://schemas.android.com/apk/res/com.me.myapp.lib"
android:key="@string/pref_vibrateOffPeriodKey"
android:title="@string/pref_VibrateOffTitle"
android:summary="@string/pref_VibrateOffSummary"
my:maxValue="@string/MaxVibratorOffPeriodInS"
my:minValue="@string/MinVibratorOffPeriodInS"
android:defaultValue="@string/DefaultVibratorOffPeriodInS" />
MyPreferencesActivity.java
public class MegalarmPreferencesActivity extends PreferenceActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
All the strings are properly defined in the lib project.
If i'm missing anything, let me know please. The preferences were working fine until i split up my project into a lib and main.
Many thanks! Sean
PreferenceActivity.onCreate can throw RuntimeExceptions internally that the IDE might catch (if configured to do so), but they are also caught internally. For instance, a missing layout_width tag in the default theme seems to cause an exception.
If the RuntimeException is not percolating back to your code don't worry about it. If it is, could you update your question to include the exception message and stack trace?
精彩评论