[英]Android Context is become null
在我的申請中
中央類的實例如下所示:
central.java:
mContext = getApplicationContext();
mMyStuff = new MyStuff(mContext);
MyStuff類需要獲取mContext才能從某些資源進行訪問。
MyStuff.java:
public class MyStuff {
private Context mContext;
public MyStuff(Context c) {
mContext = c;
}
....
private ActionCustom MyAction = new ActionCustom(mContext);
問題是,即使c不為空,mContext始終為空。 我期待在做新的MyStuff(mContext)時
問題是,即使c不為空,mContext始終為空
因為目前:
private ActionCustom MyAction = new ActionCustom(mContext);
在調用MyStuff
類構造器之前執行的行,其中mContext
對象的初始化已完成。
這樣做:
private ActionCustom MyAction;
public MyStuff(Context c) {
mContext = c;
MyAction = new ActionCustom(mContext);
}
代替
public MyStuff(Context c) {
mContext = c;
}
嘗試
public MyStuff(Context c) {
this.mContext = c;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.