سؤال

I am trying to incorporate a guided tour in my Android application, but I have run into a very simple error that I cannot solve. This error is in my XML file. Here it is:

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)

Here is my entire XML file:

<?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" />

Here is part of my ShowcaseView class:

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();
}

What am I doing wrong here? I can't find the solution. Any help regarding this issue is appreciated.

هل كانت مفيدة؟

المحلول

you need to define a constructor in your custom view that takes context and AttributeSet as params:

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

}

It is needed to construct the object from xml file.

Just add this constructor to your class and include additional code if needed.

See the related part of logcat output that complains about non-existence of this constructor:

 Exception Details java.lang.NoSuchMethodException: com.rohit.ShowcaseView.<init>
 (android.content.Context, android.util.AttributeSet)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top