2015-07-03 81 views
5

在我的应用程序Android的背景信息成为空

类中心的实例为象下面这样:

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); 

问题是mContext总是甚至在C空不空。我期待这样做的新的MyStuff(mContext)

回答

-1

而不是

public MyStuff(Context c) { 
    mContext = c; 
} 

当尝试

public MyStuff(Context c) { 
    this.mContext = c; 
} 
+0

很抱歉,但它不工作。我在this.mContext = c上放了一个断点,但它永不停止 – Seb

+3

请参阅K的答案。他可能是正确的。 – Laurens

+0

在这种情况下,“mContext = c”和“this.mContext = c”有什么区别? –

6

问题是mContext总是甚至在C空非空

因为目前:

private ActionCustom MyAction = new ActionCustom(mContext); 

在调用之前执行的行MyStuff类构造函数,其中mContext对象的初始化完成。

做得一样:

private ActionCustom MyAction; 
public MyStuff(Context c) { 
    mContext = c; 
    MyAction = new ActionCustom(mContext); 
}