Nomi Gd Nomi Gd - 6 months ago 167
Android Question

What's that "MainActivity.this is not enclosing class" error actually?

I'm having problem in executing the following code.

Actually I wanted to use an image as a link to another page or activity. How it's done

and

what's that "MainActivity.this is not enclosing class" problem actually?
Here is a code

I have 2 classes, 1st is MainActivity

and

2nd is MainActivity2

both are in Single MainAcitvity.java file

MainActivity:

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageButton;
import android.widget.Toast;
import android.content.Intent;
import android.util.Log;


public class MainActivity extends AppCompatActivity {
ImageButton androidImageButton;
@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});





androidImageButton = (ImageButton) findViewById(R.id.imageButton_android);
androidImageButton.setOnClickListener (new View.OnClickListener(){

@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "It works", Toast.LENGTH_LONG).show();
}

});



}

public boolean onCreateOptionsMenu (Menu menu){
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

public boolean onOptionsItemSelected (MenuItem item){
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}


MainActivity2:

class MainActivity2 extends MainActivity implements OnClickListener, View.OnClickListener {

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

ImageButton Btn = (ImageButton) findViewById(R.id.imageButton_android);
Btn.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public void onClick(View v) {
Log.i("clicks","You Clicked B1");
Intent i;
i = new Intent( MainActivity.this, MainActivity2.class); *//The problem is here wih MainActivity.this//
startActivity(i);
}
}


Can any one please resolve the error and help me out with the code.

Answer

An enclosing class is exactly what it sounds like - it's a class that encloses (not inherits) the class at the given statement. In order to reference the enclosing class instance, the this keyword must be prefixed with the class name - hence MainActivity.this.

class ABC {
    class XYZ extends Activity {
    } 
}

In the simple example above, ABC is the enclosing class of XYZ.

Your error is telling you that MainActivity is not an enclosing class at the statement location, and so the this instance of that class cannot be accessed.

Your MainActivity2 class inherits from MainActivity, but there is no enclosing class at the Intent(...) statement. Since the Intent() constructor requires a Context parameter and your MainActivity2 this instance inherits from Context (Context -> Activity -> MainActivity -> MainActivity2), you can just use this as the parameter:

So instead of:

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

use:

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