我在网上找到了一些使用Java的指南,这些指南试图从android获取默认的Drawable listDivider

将代码转换为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)。

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

编辑:我觉得它可能与主题有关。这是主题的设置,我只在应用程序中使用一个 Activity ,所有操作都由片段完成。
<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" />

编辑2;我还创建了一个仅运行代码块的新应用程序,但出现了同样的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()

07-24 09:15