English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
There are two cases when writing statements:
Toast.makeText(AlarmActivity.this,"Alarm clock cancelled", Toast.LENGTH_SHORT); <pre name="code" class="java">Toast.makeText(this,"Alarm clock5Start after seconds, Toast.LENGTH_SHORT);
Searched for 'what's difference between this and Activity.this' in English on Google and finally got the result (actually, I used ClassName.this and this in Java later, so I searched for Activity.this, and there was no result. I need to be flexible and improve my search skills).
Found the answer on StackOverFlow:
http://stackoverflow.com/questions/10102151/what's-the-difference-between-this-and-activity-this
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) Intent intent = new Intent(FirstActivity.this, SecondActivity());
this refers to your current object. In your case you must have implemented the intent in an inner class ClickEvent, and that's what it points to.
Activity.this points to the instance of the Activity you are currently in.
This is a reference to your current object, in your example, you must implement intent in the ClickEvent inner class, which points to ClickEvent, not the Activity you want to pass.
Activity.this points to the instance of the Activity name you filled in, which is also a reference.
In fact, this is the basic knowledge of Java, I forgot myself.
This is the current object, it is OK to use it directly in Activity. When this is used in an anonymous inner class, the current object becomes the new inner class, and if the thing you pass is the whole Activity, you need to use Activity.this.
Button b.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlarmActivity.this, "Alarm clock"5Start after seconds, Toast.LENGTH_SHORT); }};
So here you need to specify which activity, move the Toast statement outside, and you can also delete AlarmActivity.
So it is still recommended by netizens to use Activity.this all the time, which is clear.
This is the material compilation of the difference between Android this and Activity.this, and more related materials will be supplemented later. Thank you all for your support to this site!
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report via email to codebox.com (replace # with @) and provide relevant evidence. Once verified, this site will immediately delete the infringing content.