Activity is not an enclosing class (android studio)

Viewed 4247

I am new to android development. I am trying to code when the button in MainActivity gets clicked, it should go to the next activity (EnableLocationActivity). I get an error that the activity (EnableLocation) is not an enclosing class.

How can I fix this?

MainActivity.java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

    Button openingButton = (Button) findViewById(R.id.btn_main_next);
    openingButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent enableLocIntent = new Intent(MainActivity.this, EnableLocationActivity.this);
            startActivity(enableLocIntent);
        }
    });

   }
}

And...

EnableLocationActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class EnableLocationActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_enable_location);
   }
}
2 Answers
Intent enableLocIntent = new Intent(MainActivity.this, EnableLocationActivity.this);

should be

Intent enableLocIntent = new Intent(MainActivity.this, EnableLocationActivity.class);

Intent constructor your using expects a Class as second argument.

Intent enableLocIntent = new Intent(MainActivity.this, EnableLocationActivity.this);

The second argument of the Intent should be EnableLocationActivity.class because .this can be used to refer current activity and EnableLocationActivity is not your current activity, it is your next activity. So your code should be:-

Intent enableLocIntent = new Intent(MainActivity.this, EnableLocationActivity.class);

Related