[問題] 關於context一些相關問題
看板AndroidDev作者ms0561188 (說說TIES OR CONDITIONS )時間10年前 (2014/07/16 15:31)推噓2(2推 0噓 8→)留言10則, 4人參與討論串1/2 (看更多)
各位大大好 最近剛學習android
有遇到以下的問題,麻煩請高手幫忙解救一下
1.最近有點不懂context到底是甚麼,有google了一下似乎是屬於Activity的父類別
例如 Toast的寫法可以寫成
toast = Toast.makeText(getApplicationContext(),
"文字", Toast.LENGTH_LONG);
卻又可以寫成
Toast function = Toast.makeText(MainActivity.this,"文字",Toast.LENGTH_SHORT);
想知道MainActivity.this和getApplicationContext()的差別
2.也是類似的問題
下段程式執行會出現錯誤
AlertDialog.Builder aa = new AlertDialog.Builder(getApplicationContext())
而下段程式卻會成功
AlertDialog.Builder aa = new AlertDialog.Builder(MainActivity.this);
想知道為什麼
附上log
android.view.WindowManager$BadTokenException: Unable to add window -- token
null is not for an application
煩請高手幫忙解惑 感恩不盡
--
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 210.61.163.55
※ 文章網址: http://www.ptt.cc/bbs/AndroidDev/M.1405495888.A.022.html
→
07/16 15:52, , 1F
07/16 15:52, 1F
→
07/16 16:09, , 2F
07/16 16:09, 2F
→
07/16 16:47, , 3F
07/16 16:47, 3F
→
07/16 17:58, , 4F
07/16 17:58, 4F
推
07/16 20:09, , 5F
07/16 20:09, 5F
→
07/16 20:30, , 6F
07/16 20:30, 6F
→
07/16 20:30, , 7F
07/16 20:30, 7F
推
07/17 03:01, , 8F
07/17 03:01, 8F
→
07/17 03:01, , 9F
07/17 03:01, 9F
→
07/21 09:18, , 10F
07/21 09:18, 10F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):