2012-04-12 8 views
1

私のファイルにコピー貼り付け後にアラームクロックソースコードを作成しようとしています コンパイル中にmContextを解決できません。このコンパイル・エラーを解決するためにアラームクロックコード:mContext変数が解決されない

protected void onAttachedToWindow() { 
    super.onAttachedToWindow(); 

    if (Log.LOGV) Log.v("onAttachedToWindow " + this); 

    if (mAttached) return; 
    mAttached = true; 

    if (mAnimate) { 
     setBackgroundResource(R.drawable.animate_circle); 
     /* Start the animation (looped playback by default). */ 
     ((AnimationDrawable) getBackground()).start(); 
    } 

    if (mLive) { 
     /* monitor time ticks, time changed, timezone */ 
     IntentFilter filter = new IntentFilter(); 
     filter.addAction(Intent.ACTION_TIME_TICK); 
     filter.addAction(Intent.ACTION_TIME_CHANGED); 
     filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 
     mContext.registerReceiver(mIntentReceiver, filter, null, mHandler); 

    } 

    /* monitor 12/24-hour display preference */ 
    mFormatChangeObserver = new FormatChangeObserver(); 

    mContext.getContentResolver().registerContentObserver(

      Settings.System.CONTENT_URI, true, mFormatChangeObserver); 

    updateTime(); 
} 

private void setDateFormat() {  

    mFormat = Alarms.get24HourMode(mContext) ? Alarms.M24 : M12; 
    mAmPm.setShowAmPm(mFormat == M12); 
} 

の下mContextを使用するコードの一部を貼り付け、私は私のコードでこの文を入れhttp://www.netmite.com/android/mydroid/2.0/packages/apps/AlarmClock/src/com/android/alarmclock/DigitalClock.java

そして私はコピーを持っている:ここでは は、コードのこの部分へのリンクです

コンパイルエラーは解決されますが、エミュレータでの起動時にアプリケーションが例外をスローし、起動せずに終了します。

このコンテキストの使い方を教えてもらえますか、それとも別の方法として書きますか?

答えて

2

mContextではなく、getContext()メソッドを使用してコンテキストを取得します。おそらくこのサンプルコードはこの部分を見逃していました。

+0

あなたは、書き込まれるコードの一部を貼り付けてくださいすることができますか?私はアンドロイドに新しいです:( –

+0

Heinrischの答えを見てください。第2の一つ。 –

+0

ありがとうたくさんありがとう。それは働いた:) –

3

mContextを開始する必要があります。これを行うにはいくつかの方法があります。あなたができる活動では:

Context mContext = this; 

または一般:

ここ
Context mContext = getContext(); 
+0

ありがとう、それは働いた:) –

+0

その後、答えを除いてください: – Heinrisch

1

あなたのアクティビティのコンテキストを追加する必要があなたのコード

の多くを必要とする私のコード

mFormat = Alarms.get24HourMode(this) ? Alarms.M24 : M12; 
     mAmPm.setShowAmPm(mFormat == M12) 

; 

を試してみてください

私の提案は、このビューまたはアクティビティでコンテキストを取得している場所で、そこにmContext変数を初期化することですそして、それは代わりにmContext使用getApplicationContext() .Hopeの

+0

http:// stackoverflow。 com/questions/987072/using-application-context-everywhere – vipin

2

に動作します、それは作品

関連する問題