-2

So i will just post the code that is giving the error if anyone has any idea as to why this doesn't work let me know, with an explanation. (do not just say that this is a bad way to code or something) (or at least if you do explain yourself). So the code below is supposed to switch to another screen when a color that someone has clicked is true!

public boolean onTouch(View v, MotionEvent event) {
   int x = (int) event.getX();
   int y = (int) event.getY();
   if(isInsideCircle(x, y) ==  true){
      //Do your things here
       if(redColor == lastColor){
// error is here   Intent i = new Intent(this, YouFailed.class);
// and here        Activity.startActivity(i);
       } else {
           addPoints++;
       }
   }else {

   }
   return true;
}

There are two errors:

The constructor Intent(DrawingView, Class<YouFailed>) is undefined

and

Cannot make a static reference to the non-static method startActivity(Intent) from the type Activity

Piyush
  • 18,895
  • 5
  • 32
  • 63
devin
  • 368
  • 1
  • 3
  • 19

3 Answers3

3

Use v for accessing startActivity method instead of trying to call non-static method in static way:

Intent i = new Intent(v.getContext(), YouFailed.class);
v.getContext().startActivity(i);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • how would i find out more about the v or its method getContext(); ? and not from the java api i cant find it there – devin Apr 20 '15 at 05:06
  • @devin: `v` is object of `View` class and `getContext()` method is define in `View` class which return `Context` by which you can access `startActivity` method. find more detail here [View.getContext ()](http://developer.android.com/reference/android/view/View.html#getContext%28%29) – ρяσѕρєя K Apr 20 '15 at 05:14
  • yes i relized that after i typed it. its actually the argument that is passed in that method thank you though – devin Apr 20 '15 at 05:56
0

It seems that error is here,

   Intent i = new Intent(this, YouFailed.class);

because in Intent constructor your first parameter is DrawingView,instead it should be

The Intent action, such as ACTION_VIEW.

So Change this to

   Intent i = new Intent(v.getContext(), YouFailed.class);
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
0

Try to use your activity context instead inline class context :

Intent i = new Intent(YourActivity.this, YouFailed.class);
YourActivity.startActivity(i);
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67