public class aa extends Activity如何在aa中取得Context类对象菜鸟一个,希望大家指点
解决方案 »
- 如何实现多个应用程序间的调用
- 动态生成webview时的问题
- 求助 按照实例打的,最后query错误
- 回报大家:带背景图的ExpandableListview界面。。。+ 源码
- android电源管理驱动中的方法是在哪里调用的?
- android中动态添加控件的问题
- PopupWindow.setSplitTouchEnabled(true);是什么意思?
- Junit单元测试
- 多线程Volley获取图片显示在VIEW上的问题
- 请问android怎么实现矩形框录像,即录像只取矩形框部分的内容
- 请教下android里如何设置gridview里scrollbar的位置呢?
- 参加了中国移动创意大赛,请支持一下
↳ android.content.Context
↳ android.content.ContextWrapper
↳ android.view.ContextThemeWrapper
↳ android.app.Activity
Context mCtx;mCtx = aa.this;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Context context=getBaseContext();
}
AbstractInputMethodService , Activity , ActivityGroup , AliasActivity , Application , ContextThemeWrapper , ExpandableListActivity , InputMethodService , IntentService , IsolatedContext , LauncherActivity , ListActivity , MockApplication , MutableContextWrapper , PreferenceActivity , RenamingDelegatingContext , Service , TabActivity
但在另一些方法中,在需要传入Context类型的值时,直接使用this却不可以,会报出返回值类型不匹配这样的问题,而使用aa.this是OK的,谁能帮我解答下,非常感谢!
恩恩,对象变了,这个时候this就不是context了。需要用aa.this指明
B b = new B(this);
一个非Activity的类B
在类B中写个构造函数public class B {
private Context mContext;
public B(XxxActivity context){
mContext = context;
}
}
或者是它的实例
你的context没有初始化,不会报空指针异常?
B b = new B(this);b.mContext.getResource();
...
一个非Activity的类B
在类B中写个构造函数
Java codepublic class B {
private Context mContext; public B(XxxActivity context){
mContext = context;
}
}
class mApp extends Application {
private static Context instance;
//重写OnCreate()方法
onCreate() {
instance = this;
}
public static Context getInstance() {
return instance;
}
}
这样,在程序任何方法都能调用程序的Context对象了。。记得还要在manifast里把application的name属性改成你的这个类名~