簡體   English   中英

Android上下文為空

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM