2015-07-03 33 views
5

Android Bağlam Benim uygulamada

merkez sınıf aşağıda benzeri örneğini edilir null olmasına edilir kaynaklar.

MyStuff.java:

public class MyStuff { 

    private Context mContext; 

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

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

konu mContext bile her zaman c null olmasıdır boş değil. Ben

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

ait

cevap

-1

yerine yeni MyStuff (mContext) yaparken

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

Üzgünüm ama işe yaramıyor. Bu noktada bir breakpoint koymuştum.mCextext = c ama asla – Seb

+3

durur ρяσѕρєя K'nın cevabını görmez. Muhtemelen doğrudur. – Laurens

+0

Bu durumda "mContext = c" ve "this.mContext = c" arasındaki fark nedir? –

6

konu mContext bile her zaman c null olmasıdır deneyin bekliyordum boş değil

Çünkü şu anda:

private ActionCustom MyAction = new ActionCustom(mContext); 

MyStuff sınıf yapıcısını çağırmadan önce satır mContext nesnesinin başlatılması gerçekleştirilir.

olarak yapın:

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