java类无法实例化为XML
我试图在我的Android应用程序中加入导游,但我遇到了一个我无法解决的非常简单的错误。此错误在我的XML文件中。这是:
The following classes could not be instantiated:
- com.rohit.ShowcaseView (Open Class, Show Exception)
Exception Details java.lang.NoSuchMethodException: com.rohit.ShowcaseView.<init>
(安卓.content.Context, 安卓.util.AttributeSet) at java.lang.Class.getConstructor0(Class.java:2730) at java.lang.Class.getConstructor(Class.java:1676) at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:469) at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:373)
这是我的整个XML文件:
<?xml version="1.0" encoding="utf-8"?>
<!-- This is our ShowCase template that we will use
whenever we want to showcase an item.
Here we can customise the colors of the showcase. -->
<com.rohit.ShowcaseView
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:showcaseview="http://schemas.安卓.com/apk/res/com.rohit"
安卓:orientation="vertical"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
showcaseview:sv_backgroundColor="@color/showcase_background"
showcaseview:sv_buttonText="@string/showcase_button_ok" />
以下是我的ShowcaseView课程的一部分:
protected ShowcaseView(Context context) {
this(context, null, R.styleable.CustomTheme_showcaseViewStyle);
}
protected ShowcaseView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// Get the attributes for the ShowcaseView
final TypedArray styled = context.getTheme()
.obtainStyledAttributes(attrs, R.styleable.ShowcaseView, R.attr.showcaseViewStyle,
R.style.ShowcaseView);
mBackgroundColor = styled
.getInt(R.styleable.ShowcaseView_sv_backgroundColor, Color.argb(128, 80, 80, 80));
int showcaseColor = styled
.getColor(R.styleable.ShowcaseView_sv_showcaseColor, Color.parseColor("#33B5E5"));
int titleTextAppearance = styled
.getResourceId(R.styleable.ShowcaseView_sv_titleTextAppearance,
R.style.TextAppearance_ShowcaseView_Title);
int detailTextAppearance = styled
.getResourceId(R.styleable.ShowcaseView_sv_detailTextAppearance,
R.style.TextAppearance_ShowcaseView_Detail);
buttonText = styled.getString(R.styleable.ShowcaseView_sv_buttonText);
styled.recycle();
metricScale = getContext().getResources().getDisplayMetrics().density;
mEndButton = (Button) LayoutInflater.from(context).inflate(R.layout.showcase_button, null);
mShowcaseDrawer = new ClingDrawerImpl(getResources(), showcaseColor);
// TODO: This isn't ideal, ClingDrawer and Calculator interfaces should be separate
mTextDrawer = new TextDrawerImpl(metricScale, mShowcaseDrawer);
mTextDrawer.setTitleStyling(context, titleTextAppearance);
mTextDrawer.setDetailStyling(context, detailTextAppearance);
ConfigOptions options = new ConfigOptions();
options.showcaseId = getId();
setConfigOptions(options);
init();
}
我做错了什么?我找不到解决办法。在此问题上的任何帮助都将不胜感激
# 1 楼答案
您需要在自定义视图中定义一个构造函数,将
context
和AttributeSet
作为参数:需要从xml文件构造对象
只需将此构造函数添加到类中,并在需要时包含其他代码
请参阅logcat输出中抱怨此构造函数不存在的相关部分: