Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Cannot instantiate the type ShakeDetector

I am following the directions just as it is describes but for some reason I get an error when writing this line:

mShakeDetector = new ShakeDetector(new OnShakeListener() {

        @Override
        public void onShake() {
            handleNewAnswer();
        }
    });
}

4 Answers

So I just solved the code by deleting the <ShakeDetector> line that was placed right after the public class MainActivity now my project is free of errors.

Did you organise your imports and declared a ShakeDetector data type at the top.

If you could tell us the exact error by looking in the error intellisense or the console/logcat tab it would help identify better

This is all it says: Cannot instantiate the type ShakeDetector ------ Thats on the line: mShakeDetector = new ShakeDetector(new OnShakeListener() {

The method of onShake() on type of new OnShakeListener(){} must override or implement a supertype method----Thats on the line: @Override public void onShake() { handleNewAnswer(); } }); }

Strange. Did you declare a ShakeDetector mShakeDetector; at the beginning?

This declares it right? private ShakeDetector mShakeDetector;

Yeah seems like its correct. Now check if you added the ShakeDetector.java class, that you downloaded from Treehouse in the right place. It should be in the same package level as your MainActivity.java class

It's in the right level

package com.example.crystalball;

import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.hardware.Sensor; import android.hardware.SensorManager; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.animation.AlphaAnimation; import android.widget.ImageView; import android.widget.TextView;

import com.example.crystalball.ShakeDetector.OnShakeListener;

public class MainActivity<ShakeDetector> extends Activity { private CrystalBall mCrystalBall = new CrystalBall(); private TextView mAnswerLabel; private ImageView mCrystalBalImage; private SensorManager mSensorManager; private Sensor mAccelerometer; private ShakeDetector mShakeDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Assign the view from the layout file
    mAnswerLabel = (TextView) findViewById(R.id.textView1);
    mCrystalBalImage = (ImageView) findViewById(R.id.imageView1);

    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mShakeDetector = new ShakeDetector(new OnShakeListener() {

        @Override
        public void onShake() {
            handleNewAnswer();
        }
    });

}

This part is the only problem it says I can't instantiate: mShakeDetector = new ShakeDetector(new OnShakeListener() {