2016-07-30 81 views
0

我有兩部分代碼。一個有錯誤,但另一個沒有。setDisplayHomeAsUpEnabled()錯誤:無法從靜態上下文中引用非靜態方法

ActionBar theActionBar = getSupportActionBar(); 
if (theActionBar != null) { 
    // error: non-static method cannot be referenced from a static context 
    ActionBar.setDisplayHomeAsUpEnabled(true); 
} 

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

有沒有人碰巧知道如何解釋這個?

+2

'ActionBar.set'相比'theActionBar.set' .. 。使用實際的實例變量,而不是類 –

+0

這是我的不好。沒有注意到錯字。非常感謝。 – Leonard

回答

1

你的錯誤說明了一切。只是改變這一行:

ActionBar.setDisplayHomeAsUpEnabled(true); 

theActionBar.setDisplayHomeAsUpEnabled(true); 

或者只是改變你的代碼看起來像這樣:

if(getSupportActionBar() != null) 
getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
+0

這是我的不好。沒有注意到錯字。非常感謝。 – Leonard

相關問題