在Android studio layout预览的时候发现一些错误:
问题一:
Rendering Problems The following classes could not be instantiated:
- com.tomra.trsort.ui.view.KeyboardEditText (Open Class, Show Exception, Clear Cache)
Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE
- com.tomra.trsort.ui.view.KeyboardEditText (Open Class, Show Exception, Clear Cache)
Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE
在预览模式,自定义View里面的一些代码会无法渲染或预览带入的一些参数不匹配,就会导致这个问题:
只要按照提示,把有问题的代码,用
if(!this.isInEditMode()) { //有问题的代码 }
包住就可以了。
如上图的提示,KeyboardEditText是自定义的View,定位到Line 44:
把initKeybord(context,attrs)改为在:
if(!this.isInEditMode()) { initKeyboard(context, attrs); }
即可。
问题二:
Exception raised during rendering: Binary XML file line #-1: Error inflating class <unknown> (Details) (9 similar errors not shown)
这个问题在Android studio 2.1.3里面,使用7.0(api level 24)的sdk会有这个问题,把Android studio升级到最新的版本2.2或者把渲染的sdk版本改为23就不会有这个问题: