写语句的时候有两种情况: Toast.makeText(AlarMactivity.this,"闹钟取消", Toast.LENGTH_SHORT); <pre
写语句的时候有两种情况:
Toast.makeText(AlarMactivity.this,"闹钟取消", Toast.LENGTH_SHORT);
<pre name="code" class="java">Toast.makeText(this,"闹钟5秒后启动", Toast.LENGTH_SHORT);
用英文在Google搜what‘s difference between this and Activity.this,终于有了结果(其实自己后面用了Java里ClassName.this和this 之前搜的是Activity.this,所以没有结果,这点自己要灵活的提高自己的搜索能力了)。
在StackOverFlow找到了答案:
Http://stackoverflow.com/questions/10102151/whats-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 thats what it points to.
Activity.this points to the instance of the Activity you are currently in.
this是你当前对象的引用,在你的例子中你肯定在内部类ClickEvent里面实现intent,他指向的是ClickEvent,而不是你要传入的Activity。
Activity.this指向你所填写的Activity名字的一个实例,也是引用。
其实这是java的基础,我自己忘了。
this作为当前对象,直接用在Activity里面是没问题的,当this在匿名内部类中使用,当前的对象就变成new的内部类,而你传入的东西如果是整个Activity的话,就要Activity.this了。
Button b.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlarmActivity.this,"闹钟5秒后启动", Toast.LENGTH_SHORT);
}};
所以在这里面需要指定是哪个activity的,Toast的那条语句移到外面,删掉AlarmActivity也行。
所以还是网友建议,全部用成Activity.this,清晰。
以上就是 Android this与Activity.this的区别的资料整理,后续继续补充相关资料谢谢大家对本站的支持!
您可能感兴趣的文章:Android开发仿扫一扫实现拍摄框内的照片功能Android设计模式系列之工厂方法模式Android中用RxJava和ViewPager实现轮播图Android设计模式系列之单例模式Android设计模式系列之组合模式Android中的设计模式Android开发性能优化总结Android 对话框sweet-alert-dialogAndroid string-array数据源简单使用Android 解决build path errors的问题
--结束END--
本文标题: Android this与Activity.this的区别
本文链接: https://www.lsjlt.com/news/23858.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0