Swicting activity when clicked anywhere on the screen

+2 vishal sharma · February 3, 2015
Is code i made right ?

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.MotionEvent;
import android.view.GestureDetector;
import android.support.v4.view.GestureDetectorCompat;

public class MainActivity extends ActionBarActivity implements GestureDetector.OnGestureListener,

    private GestureDetectorCompat gesturedetector;

    protected void onCreate(Bundle savedInstanceState) {

        this.gesturedetector = new GestureDetectorCompat(this,this);
 public boolean onSingleTapConfirmed(MotionEvent e) {
        Intent i = new Intent(this,MainActivity2.class);
        return true;

this code does not  switching activity and i think its because i did not include view parameters plz help me i just wanna switch between activities when clicked anywhere on the screen.

Post a Reply


Oldest  Newest  Rating
+1 Nicholas Eason · February 3, 2015
First, when you post code, use the Code button, it looks like a piece of paper with <> on it. It looks much cleaner, and is easier to read.

Second, I'm pretty sure your error is here:

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

Try saying: 
Intent i = new Intent(MainActivity.this,MainActivity2.class);

From what i think is happening, you're passing just a standalone "this" which will take the "this" from the method, and what you want is a context, or your class. Specifically the constructor you're using is:  (From the android website)
Intent(Context packageContext, Class<?> cls)

The Context is just 

So, in your case, by saying 
Intent i = new Intent(this,MainActivity2.class);

You're really saying:

Intent i = new Intent(MotionEvent e,MainActivity2.class);

Which is an invalid parameter.
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator