如何在android中向XML布局添加继承类
我正在关注这个链接: 我扩展了链接中提到的如何在android中向XML布局添加继承类,android,Android,我正在关注这个链接: 我扩展了链接中提到的微调器类。下面是我的代码: public class SpinnerTrigger extends Spinner { public SpinnerTrigger(Context context) { super(context); // TODO Auto-generated constructor stub } @Override public void setSelection(int posi
微调器
类。下面是我的代码:
public class SpinnerTrigger extends Spinner {
public SpinnerTrigger(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public void setSelection(int position, boolean animate) {
ignoreOldSelectionByReflection();
super.setSelection(position, animate);
}
private void ignoreOldSelectionByReflection() {
try {
Class<?> c = this.getClass().getSuperclass().getSuperclass().getSuperclass();
Field reqField = c.getDeclaredField("mOldSelectedPosition");
reqField.setAccessible(true);
reqField.setInt(this, -1);
} catch (Exception e) {
Log.d("Exception Private", "ex", e);
// TODO: handle exception
}
}
@Override
public void setSelection(int position) {
ignoreOldSelectionByReflection();
super.setSelection(position);
}
}
如果我遗漏了什么,请建议我。将此添加到您的布局中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:tag="http://schemas.android.com/apk/res/com.mypack.cc" <!-- HERE -->
请包括错误的整个堆栈跟踪
Caused by: android.view.InflateException: Binary XML file line #12:Error inflating class com.mypack.cc.SpinnerTrigger
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:tag="http://schemas.android.com/apk/res/com.mypack.cc" <!-- HERE -->