Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Context is become null

Tags:

java

android

In my application

the class central is instantiate as like below :

central.java :

mContext = getApplicationContext();
mMyStuff = new MyStuff(mContext);

MyStuff class need to get the mContext to access from some resources.

MyStuff.java :

public class MyStuff {

    private Context  mContext;

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

    ....    
    private ActionCustom MyAction = new ActionCustom(mContext);

issue is that mContext is always null even in c is not null. I was expecting that when doing the new MyStuff(mContext)

like image 449
Seb Avatar asked Jul 03 '15 07:07

Seb


1 Answers

issue is that mContext is always null even in c is not null

Because currently :

private ActionCustom MyAction = new ActionCustom(mContext);

line executed before calling MyStuff class constructor where initialization of mContext object is done .

Do it as:

private ActionCustom MyAction;
public MyStuff(Context c) {
    mContext = c;
    MyAction = new ActionCustom(mContext);
}
like image 95
ρяσѕρєя K Avatar answered Sep 23 '22 14:09

ρяσѕρєя K