那是什么"MainActivity.this not enclosing class"?实际上是错误的吗? [英] 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
和
实际上"MainActivity.this不包含类"是什么问题?这是代码
what's that "MainActivity.this is not enclosing class" problem actually? Here is a code
我有2个班级,第一个是MainActivity
I have 2 classes, 1st is MainActivity
和
第二个是MainActivity2
2nd is MainActivity2
两者都在Single MainAcitvity.java文件中
both are in Single MainAcitvity.java file
MainActivity:
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:
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.
推荐答案
封闭类的确切含义是-这是一个封闭的类(不是继承的)给定语句中的类.为了引用封闭的类实例, this 关键字必须以类名作为前缀-因此是 MainActivity.this
.
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 {
}
}
在上面的简单示例中, ABC
是 XYZ
的封闭类.
In the simple example above, ABC
is the enclosing class of XYZ
.
您的错误告诉您 MainActivity
不是语句位置的封闭类,因此无法访问该类的 this
实例.
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.
您的 MainActivity2
类继承,但是在 Intent中没有 enclosing 类(...)
语句.由于 Intent()
构造函数需要一个 Context
参数,并且您的 MainActivity2
this
实例继承自 Context
(上下文->活动-> MainActivity-> MainActivity2),您可以仅使用 this
作为参数:
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:
所以代替:
i = new Intent( MainActivity.this, MainActivity2.class);
使用:
i = new Intent(this, MainActivity2.class);
这篇关于那是什么"MainActivity.this not enclosing class"?实际上是错误的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!