我想将我在画布上绘制的内容设置为ListView
中显示的每个元素的背景。绘图取决于元素的内容,并且必须在Canvas
上,这就是我无法从ListView元素的laout xml中设置它的原因。我需要使用View#setBackgroundDrawable
。
在单视图元素上完成时可以正常工作,但是当我在ListViewAdapter
中尝试时,我会收到非常奇怪的错误。以下是我的适配器getView
方法的代码:
public View getView(int position, View listRowView, ViewGroup parent) {
if (listRowView == null) {
listRowView = inflater.inflate(R.layout.slide_layout_row, null);
}
BackgroundDrawable drawable = BackgroundDrawable.getBackgroundDrawableFromCache(activity, list.get(position));
listRowView.setBackgroundDrawable(drawable);
return listRowView;
}
如您所见,我尝试使用缓存避免可能的内存不足问题,但删除缓存不会改变最终结果。我的列表视图中有大约100个元素,如果我尝试运行应用程序,我会在日志中获得这个奇怪的垃圾:
Fatal signal 11 (SIGSEGV) at 0x00000004 (code=1), thread 20514 (android.widgets)
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** Build fingerprint: 'samsung/yakjuxw/maguro:4.2.1/JOP40D/I9250XWMA2:user/release-keys' Revision: '9' pid: 20514, tid: 20514, name: android.widgets >>> com.cappucino.android.widgets <<< signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000004
r0 bece0630 r1 400520a4 r2 40052008 r3 00000000
r4 40052008 r5 00000001 r6 00000000 r7 40cd7a70
r8 bece0670 r9 40cd7a68 sl 400dd020 fp bece0684
ip 402522f5 sp bece0630 lr 40ab5d59 pc 40ab5c7c cpsr 28000030
d0 3f8000003f800000 d1 4434000000000000
d2 4434000042480000 d3 0000000144940000
d4 4411000000000000 d5 4411800044340000
d6 00000003000000ff d7 5da8eae800000013
d8 0000000000000000 d9 0000000000000000
d10 0000000000000000 d11 0000000000000000
d12 0000000000000000 d13 0000000000000000
d14 0000000000000000 d15 0000000000000000
d16 000000c14170b8f8 d17 000000c140e659a0
d18 000000000196e158 d19 0000000000000000
d20 400147b000000000 d21 40028f5c20000000
d22 0000000000000000 d23 40028f5c20000000
d24 3ff0000000000000 d25 0000000000000000
d26 0000000000000000 d27 c038000000000000
d28 0000000000000000 d29 3ff0000000000000
d30 3ff0000000000000 d31 3ff0000000000000
scr 60000090 backtrace:
#00 pc 0001ac7c /system/lib/libhwui.so
#01 pc 0001ad55 /system/lib/libhwui.so
#02 pc 0005b2ff /system/lib/libandroid_runtime.so
#03 pc 0001e290 /system/lib/libdvm.so (dvmPlatformInvoke+112)
#04 pc 0004d411 /system/lib/libdvm.so (dvmCallJNIMethod(unsigned int const*, JValue*, Method const*, Thread*)+396)
#05 pc 000276e4 /system/lib/libdvm.so
#06 pc 000207d8 [stack] stack:
bece05f0 5c35bf08
bece05f4 5c35bf08
bece05f8 40e55828 [heap]
bece05fc 2f40001d
bece0600 00000001
bece0604 bece0670 [stack]
bece0608 40cd79d0
bece060c 400dd020
bece0610 bece0684 [stack]
bece0614 407ed061 /system/lib/libdvm.so
bece0618 c0000000
bece061c 00000000
bece0620 00000001
bece0624 5dca4348
bece0628 df0027ad
bece062c 00000000
#00 bece0630 00000029
bece0634 00000000
bece0638 40e59558 /dev/ashmem/dalvik-heap (deleted)
bece063c 40e57ed8 /dev/ashmem/dalvik-heap (deleted)
bece0640 5d99d7b0
bece0644 40052008
bece0648 00000001
bece064c 00000000
bece0650 40cd7a70
bece0654 40ab5d59 /system/lib/libhwui.so
#01 bece0658 00000000
bece065c 40ab5d45 /system/lib/libhwui.so
bece0660 400dd010
bece0664 40252301 /system/lib/libandroid_runtime.so
#02 bece0668 56d9b160 /dev/ashmem/dalvik-LinearAlloc (deleted)
bece066c 407cf294 /system/lib/libdvm.so (dvmPlatformInvoke+116) memory near r0:
bece0610 bece0684 407ed061 c0000000 00000000
bece0620 00000001 5dca4348 df0027ad 00000000
bece0630 00000029 00000000 40e59558 40e57ed8
bece0640 5d99d7b0 40052008 00000001 00000000
bece0650 40cd7a70 40ab5d59 00000000 40ab5d45
bece0660 400dd010 40252301 56d9b160 407cf294
bece0670 40cd7a68 00000001 40e7eab8 00000008
bece0680 4170d90c 407fe415 40cd7a68 5895c109
bece0690 402522f5 400dd020 e0e00019 00000000
bece06a0 5dcadae8 40193228 40de2d88 40de2d88
bece06b0 c0000000 40de2d88 00000000 5dcada18
bece06c0 00000000 40159813 40de2d88 00000000
bece06d0 5dcada18 00000000 40de2d88 40154f15
bece06e0 40de2e1c 40812cc7 bece06fc 000000c1
bece06f0 40e7eab8 40de2e1c 00000000 00000000
bece0700 bece0760 5dcad9b0 40e659a0 000000c1 memory near r1:
40052084 00000000 00000000 00000000 00000000
40052094 3f800000 47200101 454f5f4c 40d4a8d0
400520a4 00000000 00000000 00000000 00000000
400520b4 00000000 00000000 00000000 00000000
400520c4 3f800000 00000000 3f800000 00000000
400520d4 00000000 3f800000 00000000 3f800000
400520e4 3f800000 3f800000 3f800000 3f800000
400520f4 47455f00 6d695f4c 20656761 4f5f4c47
40052104 455f5345 695f4c00 6567616d 7478655f
40052114 00000000 41400000 3f800000 00000000
40052124 00000000 00000000 00000000 00000000
40052134 00000000 00000000 00000000 00000000
40052144 ff000000 00000000 40800000 04000000
40052154 51321fc8 00000000 00000000 5dca4008
40052164 40ac6e98 00000000 00000000 00000007
40052174 00000004 40ac6ee0 00000000 00000000 memory near r2:
40051fe8 00000000 00000000 00000000 00000000
40051ff8 00000000 00000000 00000000 0000024b
40052008 40ac6f50 000002d0 000004a0 3b360b61
40052018 00000000 00000000 00000000 00000000
40052028 badd67c9 00000000 00000000 00000000
40052038 00000000 bf800000 00000000 bf800000
40052048 3f800000 80000000 3f800000 53450001
40052058 3f800000 00000000 00000000 00000000
40052068 00000000 3f800000 00000000 00000000
40052078 00000000 00000000 3f800000 00000000
40052088 00000000 00000000 00000000 3f800000
40052098 47200101 454f5f4c 40d4a8d0 00000000
400520a8 00000000 00000000 00000000 00000000
400520b8 00000000 00000000 00000000 3f800000
400520c8 00000000 3f800000 00000000 00000000
400520d8 3f800000 00000000 3f800000 3f800000 memory near r4:
40051fe8 00000000 00000000 00000000 00000000
40051ff8 00000000 00000000 00000000 0000024b
40052008 40ac6f50 000002d0 000004a0 3b360b61
40052018 00000000 00000000 00000000 00000000
40052028 badd67c9 00000000 00000000 00000000
40052038 00000000 bf800000 00000000 bf800000
40052048 3f800000 80000000 3f800000 53450001
40052058 3f800000 00000000 00000000 00000000
40052068 00000000 3f800000 00000000 00000000
40052078 00000000 00000000 3f800000 00000000
40052088 00000000 00000000 00000000 3f800000
40052098 47200101 454f5f4c 40d4a8d0 00000000
400520a8 00000000 00000000 00000000 00000000
400520b8 00000000 00000000 00000000 3f800000
400520c8 00000000 3f800000 00000000 00000000
400520d8 3f800000 00000000 3f800000 3f800000 memory near r7:
40cd7a50 00000000 40cd7a84 587060b4 56d9b160
40cd7a60 00000006 00000000 40052008 00000000
40cd7a70 40cd7aa4 5870a2ca 56d9ca20 587060b4
40cd7a80 00000000 40052008 41749c38 00000000
40cd7a90 40cd7b28 5872693c 56ee96e0 5870a2ca
40cd7aa0 00000000 4173d8b0 4171c528 4173e720
40cd7ab0 41749c38 417573f8 40cd7afc 5859b240
40cd7ac0 56d90958 5859b20c 00000000 00000000
40cd7ad0 587266b2 4173dc08 00000000 4173f1c0
40cd7ae0 00000000 00000001 40cd7b28 4175fdf0
40cd7af0 00000008 00000000 40cd7b28 58726910
40cd7b00 4173f120 4171c528 4173e720 4173d8b0
40cd7b10 4173dc08 40cd7b9c 587277a8 56efd4f0
40cd7b20 5872693c 00000000 4173f120 4171c528
40cd7b30 4173d8b0 5868e060 4173dc08 4173dc48
40cd7b40 4173e720 00000000 00000000 4173e570 memory near r8:
bece0650 40cd7a70 40ab5d59 00000000 40ab5d45
bece0660 400dd010 40252301 56d9b160 407cf294
bece0670 40cd7a68 00000001 40e7eab8 00000008
bece0680 4170d90c 407fe415 40cd7a68 5895c109
bece0690 402522f5 400dd020 e0e00019 00000000
bece06a0 5dcadae8 40193228 40de2d88 40de2d88
bece06b0 c0000000 40de2d88 00000000 5dcada18
bece06c0 00000000 40159813 40de2d88 00000000
bece06d0 5dcada18 00000000 40de2d88 40154f15
bece06e0 40de2e1c 40812cc7 bece06fc 000000c1
bece06f0 40e7eab8 40de2e1c 00000000 00000000
bece0700 bece0760 5dcad9b0 40e659a0 000000c1
bece0710 40e7eab8 000000c2 00000000 00000000
bece0720 00000000 00000000 41749c38 000000c1
bece0730 407d42c0 56d9b160 41749c38 40de2d88
bece0740 00000004 407d42c0 00000006 40cd7a54 memory near r9:
40cd7a48 4171c988 00000000 00000000 40cd7a84
40cd7a58 587060b4 56d9b160 00000006 00000000
40cd7a68 40052008 00000000 40cd7aa4 5870a2ca
40cd7a78 56d9ca20 587060b4 00000000 40052008
40cd7a88 41749c38 00000000 40cd7b28 5872693c
40cd7a98 56ee96e0 5870a2ca 00000000 4173d8b0
40cd7aa8 4171c528 4173e720 41749c38 417573f8
40cd7ab8 40cd7afc 5859b240 56d90958 5859b20c
40cd7ac8 00000000 00000000 587266b2 4173dc08
40cd7ad8 00000000 4173f1c0 00000000 00000001
40cd7ae8 40cd7b28 4175fdf0 00000008 00000000
40cd7af8 40cd7b28 58726910 4173f120 4171c528
40cd7b08 4173e720 4173d8b0 4173dc08 40cd7b9c
40cd7b18 587277a8 56efd4f0 5872693c 00000000
40cd7b28 4173f120 4171c528 4173d8b0 5868e060
40cd7b38 4173dc08 4173dc48 4173e720 00000000 memory near sl:
400dd000 00000000 00000000 00000000 00000453
400dd010 587060b4 40cd7a68 56d9ca20 58d69000
400dd020 00000000 00000000 bece07a8 00000000
400dd030 bece07dc 00000001 00010008 407d42c0
400dd040 00000000 00000000 402b4570 40cd2300
400dd050 00000000 00000000 00000000 00006000
400dd060 00000000 4012f3dc 407cf400 407d42c0
400dd070 00000000 407d83bc 407d8430 407d82e0
400dd080 407d8300 407d835c 00000000 00000000
400dd090 00000000 00000028 00000000 00000000
400dd0a0 00000000 00000000 00002000 4085f8d4
400dd0b0 00000000 00000000 00000007 40d3e130
400dd0c0 00000001 00000080 00000200 00000000
400dd0d0 00000000 00000000 00000000 00000000
400dd0e0 00000000 00000000 00000000 00000000
400dd0f0 00000000 00000000 00000000 00000000 memory near fp:
bece0664 40252301 56d9b160 407cf294 40cd7a68
bece0674 00000001 40e7eab8 00000008 4170d90c
bece0684 407fe415 40cd7a68 5895c109 402522f5
bece0694 400dd020 e0e00019 00000000 5dcadae8
bece06a4 40193228 40de2d88 40de2d88 c0000000
bece06b4 40de2d88 00000000 5dcada18 00000000
bece06c4 40159813 40de2d88 00000000 5dcada18
bece06d4 00000000 40de2d88 40154f15 40de2e1c
bece06e4 40812cc7 bece06fc 000000c1 40e7eab8
bece06f4 40de2e1c 00000000 00000000 bece0760
bece0704 5dcad9b0 40e659a0 000000c1 40e7eab8
bece0714 000000c2 00000000 00000000 00000000
bece0724 00000000 41749c38 000000c1 407d42c0
bece0734 56d9b160 41749c38 40de2d88 00000004
bece0744 407d42c0 00000006 40cd7a54 4170d90c
bece0754 407f466d 00000004 00000000 00001c01 memory near ip:
402522d4 b00447a0 6811bd10 b5104610 46196a4c
402522e4 bd1047a0 b5086810 46106a83 bd084798
402522f4 46106811 6accb510 47a04619 b51fbd10
40252304 68119c09 f8dd980a 9401c020 46109002
40252314 c000f8cd 6b0c9a06 9b074619 b00447a0
40252324 b51fbd10 68119c09 f8dd980a 9401c020
40252334 46109002 c000f8cd 6b4c9a06 9b074619
40252344 b00447a0 b513bd10 68119c06 94009807
40252354 46109001 46196d0c 9b059a04 bd1c47a0
40252364 ee01b513 eddd3a10 eef80a04 ed9d6ac1
40252374 eddd0a05 68115a06 9b074610 eeb89301
40252384 eef87ae0 ee177ac0 eeb82a10 ee176ae5
40252394 ed8d3a90 6d0c6a00 1a90ee16 bd1c47a0
402523a4 4610b510 9a026811 46196b8c bd1047a0
402523b4 46106811 6bccb510 47a04619 b510bd10
402523c4 68114610 6c0c9a02 47a04619 b510bd10 memory near sp:
bece0610 bece0684 407ed061 c0000000 00000000
bece0620 00000001 5dca4348 df0027ad 00000000
bece0630 00000029 00000000 40e59558 40e57ed8
bece0640 5d99d7b0 40052008 00000001 00000000
bece0650 40cd7a70 40ab5d59 00000000 40ab5d45
bece0660 400dd010 40252301 56d9b160 407cf294
bece0670 40cd7a68 00000001 40e7eab8 00000008
bece0680 4170d90c 407fe415 40cd7a68 5895c109
bece0690 402522f5 400dd020 e0e00019 00000000
bece06a0 5dcadae8 40193228 40de2d88 40de2d88
bece06b0 c0000000 40de2d88 00000000 5dcada18
bece06c0 00000000 40159813 40de2d88 00000000
bece06d0 5dcada18 00000000 40de2d88 40154f15
bece06e0 40de2e1c 40812cc7 bece06fc 000000c1
bece06f0 40e7eab8 40de2e1c 00000000 00000000
bece0700 bece0760 5dcad9b0 40e659a0 000000c1 code around pc:
40ab5c5c f7f60008 4620fd76 4010e8bd bb92f00a
40ab5c6c 4601bd10 b085b5f0 3f9cf851 46684604
40ab5c7c f7f8685f f8d4fa51 a801109c 0501f007
40ab5c8c 0602f007 f7f83108 0739fa47 9f01d520
40ab5c9c 3a06ed97 2a07ed97 2ac3eefd 1a08ed97
40ab5cac 0a09ed97 1ac2eefd 0a90ee12 0ac1eefd
40ab5cbc 1a90ee11 7ac0eefd 2a90ee10 3a90ee17
40ab5ccc efa2f7ef f1049900 312c000c ff12f7f9
40ab5cdc 1094f8d4 1e489f01 0094f8c4 4638b117
40ab5cec ee1ef7ef 009cf8d4 f7ffb108 f8c4ffa5
40ab5cfc b115709c f8842201 b1a621dc 46696826
40ab5d0c f8d6a802 f7f860d0 a901fa07 f7f8a803
40ab5d1c 4620fa03 aa03a902 a80347b0 fd11f7f6
40ab5d2c f7f6a802 a801fd0e fd0bf7f6 f7f64668
40ab5d3c 4628fd08 bdf0b005 460db538 2d014604
40ab5d4c 2501bfb8 4620e002 ff8bf7ff 3094f8d4 code around lr:
40ab5d38 f7f64668 4628fd08 bdf0b005 460db538
40ab5d48 2d014604 2501bfb8 4620e002 ff8bf7ff
40ab5d58 3094f8d4 dcf842ab f8d0bd38 2b013094
40ab5d68 f7ffdd01 4770bf80 4604b570 b0a86800
40ab5d78 91014616 46206981 46204788 f7fb9901
40ab5d88 f8d4f80b f7f30158 f894fb04 b11331dc
40ab5d98 f7fb4620 f8d4fd75 6f69509c c90fad05
40ab5da8 000fe885 f7fa4628 6823fd70 f8d34620
40ab5db8 479020d8 6820b148 20d4f8d0 47904620
40ab5dc8 46024629 f7fb4620 edddfcdc eddd4a05
40ab5dd8 eebd3a06 eddd4ae4 eddd2a07 68211a08
40ab5de8 f8d14620 eebd50d8 ed8d3ae3 eebd4a0d
40ab5df8 ed8d2ae2 eebd3a0e ed8d1ae1 ed8d2a0f
40ab5e08 47a81a10 059cf104 f88d4629 a802004c
40ab5e18 f984f7f8 ed909802 edd07a08 ee370a06
40ab5e28 a8020a60 7ac0eefd 7a11edcd fc8bf7f6
I/BootReceiver(390): Copying /data/tombstones/tombstone_09 to DropBox(SYSTEM_TOMBSTONE)
I/ActivityManager(390): Process com.cappucino.android.widgets (pid 20514) has died.
I/WindowState(390): WIN DEATH: Window{42bcd680 u0 com.cappucino.android.widgets/com.cappucino.android.widgets.MainActivity}
W/ActivityManager(390): Force removing ActivityRecord{41b32568 u0 com.cappucino.android.widgets/.MainActivity}: app died, no saved state
我很抱歉这个令人难以置信的长回程,但事实就是如此。在logcat中获取所有内容后,应用程序关闭。大多数内容都打印在常规日志过滤器中,而不是特定于应用程序。
当我注释掉这一行:listRowView.setBackgroundDrawable(drawable);
应用程序启动正常但我没有得到我想要的结果。
有什么建议我可以解决这个问题吗?任何想法问题实际上是什么?
修改
似乎问题在于,在调用getView
方法时,列表视图元素的画布未完全初始化。如何在画布初始化之后调用方法(这必须在渲染ListView
单元格之后)。
编辑2
行布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- ListRow Left side Thumbnail image -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/inner_layout">
<LinearLayout
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:background="@android:color/transparent"
android:padding="3dip" >
<ImageView
android:id="@+id/thumbnail_image"
android:layout_width="60dp"
android:layout_height="90dp"
android:background="@android:color/black"
android:contentDescription="@string/image_thumb_desc"
android:padding="1dp"
android:src="@drawable/default_thumb" />
</LinearLayout>
<!-- Title Of Book -->
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:background="@android:color/transparent"
android:textSize="15dip"
android:textStyle="bold"
android:typeface="sans" />
<!-- Author Name -->
<TextView
android:id="@+id/author"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail"
android:background="@android:color/transparent"
android:textColor="@android:color/darker_gray"
android:textSize="10dip" />
<!-- Arrow -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/arrow" />
</RelativeLayout>
</RelativeLayout>
答案 0 :(得分:1)
似乎问题在于画布的事实 getView时列表视图元素未完全初始化 方法被调用。如何在画布之后调用方法 初始化(这必须是在他呈现ListView单元格之后)。
不是不是。问题肯定与您的自定义类BackgroundDrawable
有关。
编辑(boris strandjev \ OP)。更具体地说,问题是我在抽象的draw
中做了除零