:Android的切換兼容庫主題有時不我目前使用下面的開關開源兼容庫應用
https://github.com/ankri/SwitchCompatLibrary
到目前爲止,它運作良好,除了在某些設備(上次報告在Android 4.0.4),應用程序首次打開活動(包含一個Switch)時會崩潰!然後在以下運行一切正常。
我已經嘗試應用的樣式(包括交換機風格)以不同的方式,但似乎沒有任何解決它:
- 在應用程序標籤
- 在活動標籤
- 在包含開關的活動的OnCreate()中
就好像第一次運行時主題未應用並且應用程序崩潰。然後在應用程序重新啓動相同的活動會好的!?
還有一種兼容庫我想,從這裏開始:https://github.com/BoD/android-switch-backport
,但我得到同樣的結果!
這裏是堆棧跟蹤,當應用程序崩潰:
------------
java.lang.NullPointerException
at de.ankri.views.Switch.onMeasure(SourceFile:514)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4704)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4704)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12723)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4704)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
at android.view.View.measure(View.java:12723)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1068)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2455)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4462)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:573)
at dalvik.system.NativeStart.main(Native Method)
------------
行代碼它發生在哪裏:
mTrackDrawable = a.getDrawable(R.styleable.Switch_track);
mTrackDrawable.getPadding(mTempRect);
它不再崩潰,因爲文本樣式缺少,因爲我手動設置它們在應用程序啓動。
這裏是在兼容性項目的各種文件的曲目風格,從主題中使用:
V11:
<style name="AppThemeDark" parent="@android:style/Theme">
<item name="switchStyle">@style/switch_dark</item>
<item name="textAppearance">@style/TextAppearance</item>
</style>
V14:
<style name="AppThemeDark" parent="@android:style/Theme.DeviceDefault">
<item name="switchStyle">@style/switch_dark</item>
<item name="textAppearance">@style/TextAppearance</item>
</style>
常見:
<style name="switch_dark">
<item name="track">@drawable/switch_track_holo_dark</item>
<item name="thumb">@drawable/switch_inner_holo_dark</item>
<item name="textOn">@string/textOn</item>
<item name="textOff">@string/textOff</item>
<item name="thumbTextPadding">12dip</item>
<item name="switchMinWidth">96dip</item>
<item name="switchPadding">16dip</item>
<item name="switchTextAppearance">@style/TextAppearance</item>
</style>
任何人使用過這些庫之一,並面臨這樣的問題?