我的android項目中有一個超類繼承了活動並實現了所有必需的接口。然後我有三個不同的活動從這個超類繼承。一旦創建活動,我將從所有子類的超類中初始化變量。超類方法:在超類中創建對象
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;
}
從子類我做的:
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方法:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
initiateVariables(this);
}
當我嘗試使用已在超被初始化的任何對象我得到一個nullpointerexception。任何人都可以向我解釋爲什麼這是?
確保超類的方法在其構造函數中被調用.. – ngesh 2012-02-28 08:51:52