类无法实例化XML

时间:2014-03-11 23:06:17

标签: java android android-xml showcaseview

我正在尝试在我的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>
(android.content.Context, android.util.AttributeSet)   at java.lang.Class.getConstructor0(Class.java:2730)   at java.lang.Class.getConstructor(Class.java:1676)   at android.view.LayoutInflater.inflate(LayoutInflater.java:469)   at android.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:android="http://schemas.android.com/apk/res/android"
    xmlns:showcaseview="http://schemas.android.com/apk/res/com.rohit"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android: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 个答案:

答案 0 :(得分:0)

您需要在自定义视图中定义一个构造函数,该构造函数将contextAttributeSet作为参数:

public ShowcaseView(Context context, AttributeSet attrs) {
    super(context, attrs);

}

需要从xml文件构造对象。

只需将此构造函数添加到您的类中,并根据需要包含其他代码。

请参阅logcat输出的相关部分,该部分抱怨此构造函数不存在:

 Exception Details java.lang.NoSuchMethodException: com.rohit.ShowcaseView.<init>
 (android.content.Context, android.util.AttributeSet)
相关问题