簡體   English   中英

Android:庫項目內部具有PreferenceScreen和自定義屬性的RuntimeException

[英]Android: RuntimeException with PreferenceScreen and Custom Attributes from inside a Library Project

我最近拆分了我的項目,並創建了一個庫項目和一個主項目。 在具有包含自定義屬性的首選項屏幕的情況下,我從我的preferences.xml中刪除了具有自定義屬性的首選項,將它們放入自己的xml文件中,然后將它們包含在preferences.xml文件中,並在主項目中重新定義了各個文件(在此處回答另一個問題中詳細介紹了此過程

項目構建並正常運行。 但是,每當我嘗試打開首選項屏幕時,我都會收到RuntimeException。 使用自定義屬性刪除首選項可解決此問題,因此我將其追溯到此為止。 不幸的是,沒有例外的有用信息。

attrs.xml(存在於lib項目中)

<?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(也在lib項目中)

<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(在lib和主項目中都定義) (僅diff是我的命名空間,一個指向lib,另一個指向主項目)

<?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);
    }
}

在lib項目中正確定義了所有字符串。

如果我有任何遺漏,請告訴我。 在我將項目拆分為lib和main之前,首選項運行良好。

非常感謝! 肖恩

PreferenceActivity.onCreate可以在內部引發IDE可能捕獲的RuntimeException(如果配置為這樣做),但它們也會在內部捕獲。 例如,默認主題中缺少layout_width標記似乎會導致異常。

如果RuntimeException沒有滲透回您的代碼,請不必擔心。 如果是,您是否可以更新問題以包括異常消息和堆棧跟蹤?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM