Leonard Leonard - 4 months ago 40
Java Question

setDisplayHomeAsUpEnabled() error: non-static method cannot be referenced from a static context

I have two parts of codes. One has the error, but the other doesn't.

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


.

getSupportActionBar().setDisplayHomeAsUpEnabled(true);


.

Does anyone happen to know how to explain this?

Answer

Your error says it all. Just change this line:

ActionBar.setDisplayHomeAsUpEnabled(true);

to

theActionBar.setDisplayHomeAsUpEnabled(true);

or just change your code to look like this:

if(getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Comments