13

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.

asked May 22, 2016 at 17:59

1 Answer 1

10

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); 
answered May 22, 2016 at 18:14
Sign up to request clarification or add additional context in comments.

4 Comments

I truly appreciate your reply thanks alot for that. According to your guide line I combined both of the classes. As far as I got this 'enclosing' means to combine these classes as 1. Am I right?
Enclosing is not telling you to combine the classes or do anything different - the word simply describes one class that sits inside of another. The error you are getting is because you are trying to access MainActivity as an enclosing class when it's not.
@adelphus Thanks dear. You solution worked for me .. by the way i have used ( new Intent(LoginActivity.this, Mainactivity.class)) and this is also working for me.
thankyou, you solve my problem which i try to solve on this last 12 hours.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.