java 这个和Activity.this有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10102151/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:38:00  来源:igfitidea点击:

What's the difference between this and Activity.this

javaandroidsyntaxandroid-activitythis

提问by user1325996

For example

例如

Intent intent = new Intent(this, SecondActivity.class);

eclipse error: The method setClass(Context, Class) in the type Intent is not applicable for the arguments (FirstActivity.ClickEvent, Class)

eclipse 错误:Intent 类型中的方法 setClass(Context, Class) 不适用于参数 (FirstActivity.ClickEvent, Class)

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);

But that will be correct. Anybody can explain the difference between those two ? Thanks.

但这将是正确的。任何人都可以解释这两者之间的区别吗?谢谢。

回答by Shubhayu

thisrefers to your current object. In your case you must have implemented the intent in an inner class ClickEvent, and thats what it points to.

this指的是您当前的对象。在您的情况下,您必须在内部类 ClickEvent 中实现了意图,这就是它指向的内容。

Activity.thispoints to the instance of the Activity you are currently in.

Activity.this指向您当前所在的 Activity 的实例。

回答by Niko

When you are pointing to this inside click event, it is pointing to the click listener.

当您指向此内部单击事件时,它指向的是单击侦听器。

回答by Samuel

Shubhayu's answer is correct, but I just want to make clear for anyone who see this question that thisand Activity.thisis the sameif you are using it directly in the activity.

Shubhayu的答案是正确的,但我只是想弄清楚的人谁看到这个问题是thisActivity.this相同的,如果你正在使用它在活动中直接

This is answered here

这是在这里回答

Example:

例子:

@Override
protected void onResume() {
    super.onResume();

    Log.d("Test", this.toString());
    Log.d("Test", MainActivity.this.toString());
}

Result:

结果:

D/Test: com.example.app.MainActivity@e923587
D/Test: com.example.app.MainActivity@e923587

回答by varun257

You are intent to transfer control from one activity to another and for that u ll have to specify an event basically and hence the error. this means the entire activity and firstactivity.this means an event occurring for example a a button clicked.........

您打算将控制权从一个活动转移到另一个活动,为此您必须基本上指定一个事件,从而指定错误。这意味着整个活动和第一个活动。这意味着一个事件发生,例如点击按钮.......