0

我正在尝试在我的 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();
}

我在这里做错了什么?我找不到解决方案。对此问题的任何帮助表示赞赏。

4

1 回答 1

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)
于 2014-03-11T23:12:54.163 回答