Android context outside of Activity class

梦想与她 提交于 2019-12-18 15:03:25

问题


I am trying to handle exceptions in my application. I am trying to log the exception, then use Toast to alert the user that there was a problem. I have this working find in all my class's that extend Activity.

However, in any class that does not extended activity I can not use the toast method as I can't get the current context. Is there a simple way to get around this or should all my class's extend Activity?


回答1:


You just pass Context When You call Non-Activity class from Activity class call Like

YourNonActivtyClass obj = new YourNonActivtyClass(Activity.this);



回答2:


Toast messages should work within the application context, i.e.

Toast.makeText(getApplicationContext(), ...);


来源:https://stackoverflow.com/questions/9725394/android-context-outside-of-activity-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!