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
What's the difference between this and Activity.this
提问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
this
refers 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.this
points 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 this
and Activity.this
is the sameif you are using it directly in the activity.
Shubhayu的答案是正确的,但我只是想弄清楚的人谁看到这个问题是this
和Activity.this
是相同的,如果你正在使用它在活动中直接。
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.........
您打算将控制权从一个活动转移到另一个活动,为此您必须基本上指定一个事件,从而指定错误。这意味着整个活动和第一个活动。这意味着一个事件发生,例如点击按钮.......