你好我試圖找出如何在下面工作的代碼及其含義:this關鍵字在下面的Android程序上下文
public Context mContext;
private Common mApp;
public Activity mActivity;
mContext = this;
mActivity = this;
mApp = (Common) mContext.getApplicationContext()
對我來說,調用一個方法,我需要創建一個類的對象如下
Context mContext = new Context();
and then i would be able to invoke the method getAppliationContext as shown above:
mContext.getApplicationContext()
有人可以解釋的是,我請了,還怎麼是這個關鍵字的應用above.From我知道什麼是這個關鍵字是對對象本身的引用。 此外,我不明白怎麼「MAPP」被用來作爲一個對象來調用下面的各種方法:
int startCount = mApp.getSharedPreferences().getInt("START_COUNT", 1);
mApp.getSharedPreferences().edit().putInt("START_COUNT", startCount+1).commit();
但它沒有被使用new關鍵字創建的,我想,我需要要執行以下操作以調用使用mApp的方法:
Common mApp = new Common();
,這樣我就能夠做到這一點
int startCount = mApp.getSharedPreferences().getInt("START_COUNT", 1);
mApp.getSharedPreferences().edit().putInt("START_COUNT", startCount+1).commit();
我覺得我在哪裏,在標題是,有沒有其他的辦法,而不只是使用new關鍵字來創建一個對象。
謝謝,這是否意味着getApplicationContext()方法返回一個Context類型的對象?並且我們將這個對象分配給refernce上下文? – kobewarui
@kobewarui是的,它返回一個類型的對象上下文 – Darish