【发布时间】:2014-02-06 06:27:26
【问题描述】:
我写了简单的一行代码来获取 android 应用程序的上下文。
除了一项活动的新项目外,别无其他。
我在 onCreate 方法中编写了这段代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Context context = getContext();
}
它要求我用 getBaseContext 替换 getContext。
为什么我不能只使用现有的 getContext?
【问题讨论】:
-
活动中的代码在哪里?
-
我已经在 OnCreate 方法中写过。
-
那么就用
getApplicationContext() -
为了更好的练习使用
getApplicationContext() -
你应该使用 getApplicationContext()