获取Android中的默认分隔符



我在网上找到了一些使用Java的指南,试图从android获得默认的DrawablelistDivider

在将代码转换为Kotlin后,我有以下内容;

val attrs = IntArray(android.R.attr.listDivider)
val ta = context.obtainStyledAttributes(attrs)
mDivider = ta.getDrawable(0)
ta.recycle()

但我遇到内存不足的异常,应用程序关闭。

我试过检查android.R.attr.listDivider,它只是一个0的列表,而R.attr.listDivider不存在(我导入了R(。

不太确定我还能尝试什么。

编辑:我觉得这可能与主题有关。这是主题的设置,我在应用程序中只使用了一个活动,一切都是用片段完成的。

<application
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar" >
</activity>
</application>

然后在styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

EDIT2;我还创建了一个新的应用程序,它只运行代码块,我得到了相同的java.lang.OutOfMemoryError: Failed to allocate a 404238828 byte allocation with 4194304 free bytes and 318MB until OOM错误

原来我初始化IntArray错误;

val attrs = intArrayOf(android.R.attr.listDivider)
val a = context.obtainStyledAttributes(attrs)
mDivider = a.getDrawable(0)
a.recycle()

最新更新