Titanium製のAndroidアプリで、TableViewで頻繁に落ちる事がたまに起こります。
たとえばこんなエラー
java.lang.ClassCastException: ti.modules.titanium.ui.LabelProxy cannot be cast to ti.modules.titanium.ui.ImageViewProxy at ti.modules.titanium.ui.widget.TiUIImageView.setProxy(TiUIImageView.java:202) ...
原因として、やたらTableViewRowを更新しているとか、スクロールの処理がトリガーになってるとか、いろんなのを探ってしまうけど、意外な盲点が。
TableViewRowのclassName指定によって落ちるのです。
classNameは、TableViewRow内で同じレイアウト/子要素を持つ場合、同じclassNameを指定する事で参照のパフォーマンスがあがる、という設定値とのことです。
var row1 = Ti.UI.createTableViewRow({ width:Ti.UI.FILL, height:Ti.UI.SIZE, className:'rowPattern1' }); row.add(Ti.UI.createLabel({text:'hello'})); var row2 = Ti.UI.createTableViewRow({ width:Ti.UI.FILL, height:Ti.UI.SIZE, className:'rowPattern1' }); row.add(Ti.UI.createLabel({text:'hello'})); ...
ですが、
- 違うレイアウト/子要素なのに同じclassName
- 同じレイアウト/子要素でも、途中で更新されてしまう(各設定値の変更でも影響する?
といった時に、上述のClassCastExceptionとなるようです。
自分も何度か出会って困る事多いので備忘録。