我的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