I am developing an Android app with multiple Activities. In order to avoid writing the same menu and Intent listener code into each Activity, I decided to create one file and implement it on every Activity like the following:
public class MainActivity extends Activity implements CommonTools{ ...
My commonTools file which contains my menu code is as follows:
import android.view.Menu;
public interface CommonTools {
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
menu.findItem(R.id.menu_home).setIntent(
new Intent(this, MainActivity.class));
menu.findItem(R.id.menu_articles).setIntent(
new Intent(this, ArticlesActivity.class));
return true;
}
}
The problem is I get an “Abstract methods do not specify a body” error message.
I have not declared anything abstract and don’t understand what the issue is.
An Interface can’t contain any code. What you need to do is extend Activity then extend that class. E.g.
Then define your activities: