Robert Ruxandrescu
Robert Ruxandrescu

Reputation: 667

Can't instantiate interface in Android, "anonymous" error

I have implemented an .aar library for my Android project that provides some speed indicators that are animated and so on. The library is called "SpeedometerView-1.0.1.aar"

Everything works well and it does its job. However, I'm trying to implement a label for my speedometers. I have used the documentation found for this library. One basic example is found here:

https://github.com/ntoskrnl/AndroidWidgets

Here's the code in the tutorial example:

Usage

Import the library to your project.

In your layout xml-file add SpeedometerGauge as shown:

<com.cardiomood.android.controls.gauge.SpeedometerGauge
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:padding="8dp"
    android:id="@+id/speedometer" />

Configure SpeedometerGauge:

private SpeedometerGauge speedometer;

  // Customize SpeedometerGauge
  speedometer = (SpeedometerGauge) v.findViewById(R.id.speedometer);

  // Add label converter
  speedometer.setLabelConverter(new SpeedometerView.LabelConverter() {
      @Override
      public String getLabelFor(double progress, double maxProgress) {
          return String.valueOf((int) Math.round(progress));
      }
  });

  // configure value range and ticks
  speedometer.setMaxSpeed(300);
  speedometer.setMajorTickStep(30);
  speedometer.setMinorTicks(2);

  // Configure value range colors
  speedometer.addColoredRange(30, 140, Color.GREEN);
  speedometer.addColoredRange(140, 180, Color.YELLOW);
  speedometer.addColoredRange(180, 400, Color.RED);

My problem is with the "Add label converter".

When I try to use that in my code, I get this error:

setLabelConverter (com.cardiomood.android.controls.gauge.SpeedometerGauge.LabelConverter) in SpeedometerGauge cannot be applied to (anonymous com.cardiomood.android.speedometer.SpeedometerView.LabelConverter)

Here's how my relevant code looks like:

    private void setCoolingIndicatorAttributes() {
        coolingIndicator.setMaxSpeed(250);
        coolingIndicator.setMajorTickStep(50);
        coolingIndicator.setMinorTicks(3);
        coolingIndicator.addColoredRange(0, 140, Color.GREEN);
        coolingIndicator.addColoredRange(140, 200, Color.YELLOW);
        coolingIndicator.addColoredRange(200, 250, Color.RED);
//         Add label converter
        coolingIndicator.setLabelConverter(new SpeedometerView.LabelConverter() {
            @Override
            public String getLabelFor(double progress, double maxProgress) {
                return String.valueOf((int) Math.round(progress));
            }
        });
    }

Again, this ^^^ doesn't work. I get the error I described:

setLabelConverter (com.cardiomood.android.controls.gauge.SpeedometerGauge.LabelConverter) in SpeedometerGauge cannot be applied to (anonymous com.cardiomood.android.speedometer.SpeedometerView.LabelConverter)

As you can see, the only difference is the word "anonymous" in front of the package name and class.

I've tried quite a few approaches to solve this, but was unsuccessful. There has to be some very easy way to solve this that for some reason escapes me. I appreciate any input.

Thanks!

EDIT: I think I've solved this:

I think I managed to resolve this: the name should be SpeedometerGauge, not SpeedometerView.

So the correct code is:

//         Add label converter
        coolingIndicator.setLabelConverter(new SpeedometerGauge.LabelConverter() {
            @Override
            public String getLabelFor(double progress, double maxProgress) {
                return String.valueOf((int) Math.round(progress));
            }
        });
    }

Upvotes: 0

Views: 104

Answers (1)

Alexandre BOURETZ
Alexandre BOURETZ

Reputation: 296

It seems that you're instantiating a SpeedometerGauge and not a SpeedometerView, so you should replace

speedometer.setLabelConverter(new SpeedometerView.LabelConverter() {

by

speedometer.setLabelConverter(new SpeedometerGauge.LabelConverter() {

Upvotes: 2

Related Questions