2012-02-28 5 views
0

私は自分のアンドロイドプロジェクトにアクティビティから継承し、必要なすべてのインターフェイスを実装するスーパークラスを1つ持っています。私はこのスーパークラスを継承する3つの異なるアクティビティを持っています。アクティビティが作成されると、すべてのサブクラスからスーパークラスの変数を初期化します。スーパークラスメソッド:サブクラスからスーパークラスでのオブジェクトの作成

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を取得します。誰が私にこの理由が説明できますか?

+0

ことを確認してください..あなたはそれが欠けているかもしれませスーパークラスのメソッドはコンストラクタ内で呼び出されます。 – ngesh

答えて

0

は、私はあなたがそれらのすべてからinitiateVariables(コンテキストc)このメソッドをスーパークラス内のすべての3 活動コンストラクタを追加し、呼び出すことをお勧め...

+0

アクティビティコンストラクタはどういう意味ですか? – user1163392

+0

スーパークラスのコンストラクタを継承すると、現在のクラスのコンストラクタの実行が完了する前に呼び出されます。http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html – ngesh

+0

これは私の助けになりますか?なぜ私の現在のやり方がうまくいかないのですか? nullpointer例外が発生するのはなぜですか? – user1163392

関連する問題