java “MainActivity.this 不是封闭类”错误实际上是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37377717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's that "MainActivity.this is not enclosing class" error actually?
提问by Nomi Gd
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
“MainActivity.this 不是封闭类”问题实际上是什么?这是一个代码
I have 2 classes, 1st is MainActivity
我有 2 个类,第一个是 MainActivity
and
和
2nd is MainActivity2
第二个是 MainActivity2
both are in Single MainAcitvity.java file
两者都在 Single MainAcitvity.java 文件中
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:
主要活动2:
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.
任何人都可以解决错误并帮助我解决代码。
采纳答案by adelphus
An enclosing classis 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 thiskeyword must be prefixed with the class name - hence MainActivity.this
.
一个封闭类正是这听起来像-这是一类封闭(不继承)在给定的语句类。为了引用封闭的类实例,this关键字必须以类名作为前缀 - 因此MainActivity.this
.
class ABC {
class XYZ extends Activity {
}
}
In the simple example above, ABC
is the enclosingclass of XYZ
.
在上面的简单的例子,ABC
是封闭类的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.
您的错误告诉您这MainActivity
不是语句位置的封闭类,因此this
无法访问该类的实例。
Your MainActivity2
class inheritsfrom MainActivity
, but there is no enclosingclass 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:
您的MainActivity2
类继承自MainActivity
,但语句中没有 封闭类Intent(...)
。由于Intent()
构造函数需要一个Context
参数,并且您的MainActivity2
this
实例继承自Context
(Context -> Activity -> MainActivity -> MainActivity2),因此您可以将其this
用作参数:
So instead of:
所以而不是:
i = new Intent( MainActivity.this, MainActivity2.class);
use:
利用:
i = new Intent(this, MainActivity2.class);