I have one superclass in my android project that inherits from activity and implements all the necessary interfaces. I then have three different activities that inherit from this superclass. I initialize the variables in the superclass from all the subclasses once the activities are created. Superclass method:
protected void initiateVariables(Context c){
dm = new DisplayMetrics();
toasty = new Toast(c);
customBuilder = new MyDialog.Builder(c);
customDialog = new Dialog(c);
customProgress = new MyProgressDialog(c);
getWindowManager().getDefaultDisplay().getMetrics(dm);
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
}
From the subclasses I do:
protected void initiateVariables(Context c) {
super.initiateVariables(c);
bFavorite = (Button) findViewById(R.id.bFavorite);
bSetting = (Button) findViewById(R.id.bSettings);
bCompass = (Button) findViewById(R.id.bCompass);
bDeparture = (Button) findViewById(R.id.bDeparture);
bInfo = (Button) findViewById(R.id.bInfo);
bBack = (Button) findViewById(R.id.bBack);
bDeparture2 = (Button) findViewById(R.id.bForward);
bAdd = (Button) findViewById(R.id.bAdd);
tvHeader = (TextView) findViewById(R.id.tvHeaderText);
flipp = (ViewFlipper) findViewById(R.id.viewFlipper);
colorBackground = (RelativeLayout) findViewById(R.id.homeBackground);
dbList = (ListView) findViewById(R.id.dbList); }
onCreate method in my subclasses:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
initiateVariables(this);
}
When I try to use any object that has been initialized in the superclass I get a nullpointerexception. Could anyone explain to me why this is?
I suggest you to add all 3 Activity constructors in the superclass and call initiateVariables(Context c) this method from all of them… May be you are missing it..