最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android this與Activity.this的區(qū)別

 更新時(shí)間:2016年09月19日 15:48:56   作者:iaiti  
這篇文章主要介紹了 Android this與Activity.this的區(qū)別的相關(guān)資料,需要的朋友可以參考下

寫語句的時(shí)候有兩種情況:

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,終于有了結(jié)果(其實(shí)自己后面用了Java里ClassName.this和this 之前搜的是Activity.this,所以沒有結(jié)果,這點(diǎn)自己要靈活的提高自己的搜索能力了)。

      在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是你當(dāng)前對象的引用,在你的例子中你肯定在內(nèi)部類ClickEvent里面實(shí)現(xiàn)intent,他指向的是ClickEvent,而不是你要傳入的Activity。

Activity.this指向你所填寫的Activity名字的一個(gè)實(shí)例,也是引用。

    其實(shí)這是java的基礎(chǔ),我自己忘了。

this作為當(dāng)前對象,直接用在Activity里面是沒問題的,當(dāng)this在匿名內(nèi)部類中使用,當(dāng)前的對象就變成new的內(nèi)部類,而你傳入的東西如果是整個(gè)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);  
}}; 

所以在這里面需要指定是哪個(gè)activity的,Toast的那條語句移到外面,刪掉AlarmActivity也行。

    所以還是網(wǎng)友建議,全部用成Activity.this,清晰。

以上就是 Android this與Activity.this的區(qū)別的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料謝謝大家對本站的支持!

相關(guān)文章

最新評論

黄骅市| 宝鸡市| 梅州市| 砚山县| 兰溪市| 临湘市| 枣庄市| 曲沃县| 海晏县| 屯昌县| 罗江县| 霞浦县| 大石桥市| 新乐市| 江安县| 汾西县| 花莲市| 濮阳市| 喀喇| 金湖县| 昭平县| 韩城市| 望都县| 长春市| 南汇区| 舞钢市| 郸城县| 郎溪县| 本溪市| 西和县| 石河子市| 四子王旗| 丰台区| 永福县| 武邑县| 谢通门县| 通道| 兴山县| 和田县| 繁昌县| 简阳市|