修复自定义布局不能预览问题
网上关于布局不能预览的问题无非就是clear cache,build,但是这并不能解决我这个项目的问题,
还有人说通过设置targetCompatibility JavaVersion.VERSION_1_8 但是这并不能解决我的问题。
在android studio Android Studio Dolphin | 2021.3.1 Canary 8中 布局的错误似乎稍微比较详细,至少告诉我是此类的具体某一个方法,
于是强迫症的我对于这种千年bug决定解决一下,方便我能够在开发的时候预览布局以便提升开发效率
The following classes could not be instantiated:
- xxx.EditSpinner (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.
If this is an unexpected error you can also try to build the project, then manually refresh the layout.
Exception Details java.lang.VerifyError:
Bad type on operand stack Exception Details:
Location: xxx/EditSpinner.initMy2Event()V @69: invokevirtual Reason: Type 'java/lang/Object' (current frame, stack[0]) is not assignable to 'android/view/View' Current Frame: bci: @69 flags: { }
locals: { 'xxxEditSpinner', 'java/lang/Object' }
stack: { 'java/lang/Object', 'xxx/EditSpinner$2' }
Bytecode: 0000000: 2ab4 0124 c101 a799 0015 2ab4 0124 c001 0000010:
a74c 2b2a ba02 1a00 00b6 021e 2ab4 0088 0000020: 2ab4 014f b602 222a b400 9305 a000 0b2a 0000030: b401 3d4c a700 082a b400 b94c 2bbb 0018 0000040:
592a b702 23b6 0227 2ab4 0124 2aba 0231 0000050:
0same_frame(@55) append_frame(@60,Object[#589])
at java.lang.Class.getDeclaredConstructors0(Class.java:-2)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:3137)
at java.lang.Class.getConstructor0(Class.java:3342)
at java.lang.Class.getConstructor(Class.java:2151)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1127)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1101)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1130)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
at android.view.LayoutIn
首先,知道是哪个方法报错了,是initMy2Event
方法,但是用View.isInEditMode()
为true 跳过设置事件,没有用,原因是class加载的时候就出错了,
错误
stack: { 'java/lang/Object', 'xxx/EditSpinner$2' }
通过everything搜索class类,使用工具逆向分析,class转smali再转java
精准的得到如下代码:
import android.view.View;
import android.view.View.OnClickListener;
class EditSpinner$2 implements OnClickListener {
final /* synthetic */ EditSpinner this$0;
EditSpinner$2(EditSpinner this$02) {
this.this$0 = this$02;
}
public void onClick(View v) {
if (EditSpinner.access$300(this.this$0) == null || !EditSpinner.access$300(this.this$0).onClick()) {
if (EditSpinner.access$200(this.this$0).getSelectedItemPosition() == 0) {
Object selectedItem = EditSpinner.access$200(this.this$0).getSelectedItem();
if (selectedItem != null) {
EditSpinner.access$100(this.this$0, selectedItem + "");
}
}
EditSpinner.access$200(this.this$0).performClick();
}
}
}
实际上没有任何毛病,对应的是
View dropDownView;
if (layoutMode == LAYOUT_MODE_TOP_LABEL_ARROW) {
dropDownView = _titleWrap;
} else {
dropDownView = imageView;
}
// layoutMode == LAYOUT_MODE_TOP_LABEL_ARROW ? _titleWrap : imageView;
dropDownView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (spinnerClickListener != null && spinnerClickListener.onClick()) {
return;
}
if (appCoSpinner.getSelectedItemPosition() == 0) {
Object selectedItem = appCoSpinner.getSelectedItem();
if (selectedItem != null) {
setTextNoNotify(selectedItem + "");//当clearText的时候又选中当前的时候,不会触发onOptionChange,所以点击就自动给它设置吧。没其他更好的办法了!
}
}
appCoSpinner.performClick();
}
});
改成if 判断 或者lambda语法都解决不了问题,不过具体哪个匿名类并没有提示了,说明匿名类被优化没了,但是得到了匿名类具体的某个方法,
因此精准的定位到了是这个监听事件设置给这个变量出现了类型识别错误,object没有点击事件的。
但是我这里定义的view,是有点击事件的,这是开发工具的bug.
Reason: Type 'java/lang/Object' (current frame, stack[0]) is not assignable to 'android/view/View'
Current Frame: bci: @67 flags: { }
locals: { 'xxx/EditSpinner', 'java/lang/Object' }
stack: { 'java/lang/Object', 'android/view/View$OnClickListener' } Bytecode:
可能是object转view出毛病了。
可以看到是点击事件有毛病
总之就是这个东西出毛病了,咋解决呢?目前只能通过不使用匿名类,而单独使用类来解决了。这样就达到了按需加载?
当然强迫症的我决定测试分析它的原因,于是我不分配dropDownView类型为view,而是分别设置点击事件,最后发现成功了
View.OnClickListener onClickListener= new OnClickListener() {
@Override
public void onClick(View v) {
if (spinnerClickListener != null && spinnerClickListener.onClickConsumed()) {
return;
}
if (appCoSpinner.getSelectedItemPosition() == 0) {
Object selectedItem = appCoSpinner.getSelectedItem();
if (selectedItem != null) {
setTextNoNotify(selectedItem + "");//当clearText的时候又选中当前的时候,不会触发onOptionChange,所以点击就自动给它设置吧。没其他更好的办法了!
}
}
appCoSpinner.performClick();
}
};
if (layoutMode == LAYOUT_MODE_TOP_LABEL_ARROW) {
_titleWrap.setOnClickListener(onClickListener);
} else {
imageView.setOnClickListener(onClickListener);
}
有意思的是flutter也有同样的问题,对三元运算赋值存在毛病。
ImageProvider imageProvider;
if (Global.profile.user.logourl.isEmpty) {
imageProvider = AssetImage('assets/images/ico_head_default.png');
} else {
imageProvider = NetworkImage(Global.profile.user.logourl);
}
Object imageProviderX;
Object obj=Global.profile.user.logourl.isEmpty?AssetImage('assets/images/ico_head_default.png'): NetworkImage(Global.profile.user.logourl);
//A value of type 'Object' can't be assigned to a variable of type 'ImageProvider<Object>'.下面这行错误
imageProvider=Global.profile.user.logourl.isEmpty?AssetImage('assets/images/ico_head_default.png'): NetworkImage(Global.profile.user.logourl);